-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriocircq.c
214 lines (187 loc) · 5.13 KB
/
priocircq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<stdio.h>
#define MAX_SIZE 4
typedef struct cq {
int queue[MAX_SIZE];
int front, rear;
} cq;
typedef struct PrioElm{
int data;
int prio;
}PrioElm;
typedef struct pq {
PrioElm queue[MAX_SIZE];
int size;
} pq;
void initPriorityQueue(pq *pq) {
pq->size = 0;
}
int isPriorityQueueEmpty(const pq *pq) {
return pq->size == 0;
}
int isPriorityQueueFull(const pq *pq) {
return pq->size == MAX_SIZE;
}
void enqueuePriorityQueue(pq *pq, PrioElm item) {
if (isPriorityQueueFull(pq)) {
printf("Priority queue is full. Cannot enqueue.\n");
} else {
int i = pq->size - 1;
while (i >= 0 && item.prio < pq->queue[i].prio) {
pq->queue[i + 1] = pq->queue[i];
i--;
}
pq->queue[i + 1] = item;
pq->size++;
printf("%d enqueued successfully.\n", item.data);
}
}
void dequeuePriorityQueue(pq *pq) {
if (isPriorityQueueEmpty(pq)) {
printf("Priority queue is empty. Cannot dequeue.\n");
} else {
printf("%d dequeued successfully.\n", pq->queue[0].data);
for (int i = 1; i < pq->size; i++) {
pq->queue[i - 1].data = pq->queue[i].data;
}
pq->size--;
}
}
void displayPriorityQueue(const pq *pq) {
if (isPriorityQueueEmpty(pq)) {
printf("Priority queue is empty.\n");
} else {
printf("Priority queue elements : ");
for (int i = 0; i < pq->size; i++) {
printf("\n");
printf("%d [%d]", pq->queue[i].data, pq->queue[i].prio);
}
printf("\n");
}
}
void prioq(){
int cchoice;
pq priorityQueue;
initPriorityQueue(&priorityQueue);
PrioElm elm;
while(cchoice!=4){
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
scanf("%d",&cchoice);
switch(cchoice){
case 1: printf("Enter element to insert : ");
scanf("%d", &elm.data);
printf("Enter Priority of element : ");
scanf("%d", &elm.prio);
enqueuePriorityQueue(&priorityQueue, elm);
break;
case 2: dequeuePriorityQueue(&priorityQueue);
break;
case 3: displayPriorityQueue(&priorityQueue);
break;
case 4: break;
}
}
}
int ElemCount=0;
void displayQueue(int x,int y,int n,int Queue[n]){
//printf("front:%d\nrear:%d\n",x,y);
if(ElemCount==0){
printf("Queue is empty\n");
}
else{
printf("Queue is:");
for(int i=x;i<=y;i++){
printf("%d ",Queue[i]);
}
printf("\n");
}
}
void initCircularQueue(cq *cq) {
cq->front = cq->rear = -1;
}
int isCircularQueueEmpty(const cq *cq) {
return cq->front == -1 && cq->rear == -1;
}
int isCircularQueueFull(const cq *cq) {
return (cq->rear + 1) % MAX_SIZE == cq->front;
}
void enqueueCircularQueue(cq *cq, int item) {
if (isCircularQueueFull(cq)) {
printf("Queue is full. Cannot enqueue.\n");
} else {
if (isCircularQueueEmpty(cq)) {
cq->front = cq->rear = 0;
} else {
cq->rear = (cq->rear + 1) % MAX_SIZE;
}
cq->queue[cq->rear] = item;
printf("%d enqueued\n", item);
}
}
void dequeueCircularQueue(cq *cq) {
if (isCircularQueueEmpty(cq)) {
printf("Queue is empty. Cannot dequeue.\n");
} else {
printf("%d dequeued\n", cq->queue[cq->front]);
if (cq->front == cq->rear) {
cq->front = cq->rear = -1;
} else {
cq->front = (cq->front + 1) % MAX_SIZE;
}
}
}
void displayCircularQueue(const cq *cq) {
if (isCircularQueueEmpty(cq)) {
printf("Queue is empty.\n");
} else {
int i = cq->front;
printf("Queue elements: ");
do {
printf("%d ", cq->queue[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (cq->rear + 1) % MAX_SIZE);
printf("\n");
}
}
void circq(){
cq circularQueue;
initCircularQueue(&circularQueue);
int pchoice,item;
while(pchoice!=4){
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit.\n");
scanf("%d", &pchoice);
switch(pchoice){
case 1: printf("Enter Element : ");
scanf("%d", &item);
enqueueCircularQueue(&circularQueue, item);
displayCircularQueue(&circularQueue);
break;
case 2: dequeueCircularQueue(&circularQueue);
displayCircularQueue(&circularQueue);
break;
case 3: displayCircularQueue(&circularQueue);
break;
case 4: break;
}
}
}
int main() {
int choice;
while(choice !=3){
printf("\n1.Circular Queue\n2.Priority Queue\n3.Exit\n");
printf("Enter Choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
circq();
break;
case 2:
prioq();
break;
case 3:
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}