-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_multiple_queue.c
More file actions
249 lines (208 loc) · 5.1 KB
/
Copy path3_multiple_queue.c
File metadata and controls
249 lines (208 loc) · 5.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>
#include <stdlib.h>
typedef struct queue
{
int *arr; // Array to store elements of all queues
int *front; // Array to store front index of all queues
int *rear; // Array to store rear index of all queues
int *next; // Array to store next free location
int free; // Beginning of the free list of queues
int k, n; // Number of queues and size of array
} KQueue;
KQueue *createKQueue(int k, int n)
{
KQueue *q = (KQueue *)malloc(sizeof(KQueue));
q->k = k;
q->n = n;
q->arr = (int *)calloc(n, sizeof(int));
q->front = (int *)malloc(k * sizeof(int));
q->rear = (int *)malloc(k * sizeof(int));
q->next = (int *)malloc(n * sizeof(int));
// Initialize all queues as empty
for (int i = 0; i < k; i++)
{
q->front[i] = -1;
q->rear[i] = -1;
}
// Initialize all spaces as free and link them together
for (int i = 0; i < n - 1; i++)
{
q->next[i] = i + 1;
}
q->next[n - 1] = -1;
q->free = 0; // Current free slot
return q;
}
void enqueue(KQueue *q, int val, int qno)
{
if (qno < 0 || qno >= q->k)
{
printf("Invalid queue number\n");
return;
}
if (q->free == -1)
{
printf("Queue is full\n");
return;
}
int i = q->free; // Current free slot
q->free = q->next[i]; // Update the free slot with next free slot
if (q->front[qno] == -1)
{
q->front[qno] = i; // First element
}
else
{
q->next[q->rear[qno]] = i; // Link new element
}
q->next[i] = -1; // Set next of this ith slot as -1 (this is the last element)
q->rear[qno] = i;
q->arr[i] = val;
printf("Enqueued %d in queue %d\n", val, qno);
}
int dequeue(KQueue *q, int qno)
{
if (qno < 0 || qno >= q->k)
{
printf("Invalid queue number\n");
return -1;
}
if (q->front[qno] == -1)
{
printf("Queue %d is empty\n", qno);
return -1;
}
int i = q->front[qno];
q->front[qno] = q->next[i];
if (q->front[qno] == -1)
{ // If queue becomes empty
q->rear[qno] = -1; // Set rear of this queue as -1
}
q->next[i] = q->free; // Link freed index to free list
q->free = i;
printf("Dequeued %d from queue %d\n", q->arr[i], qno);
return q->arr[i];
}
void display(KQueue *q, int qno)
{
if (qno < 0 || qno >= q->k)
{
printf("Invalid queue number\n");
return;
}
if (q->front[qno] == -1)
{
printf("Queue %d is empty\n", qno);
return;
}
printf("Queue %d: ", qno);
int i = q->front[qno];
while (i != -1)
{
printf("%d ", q->arr[i]);
i = q->next[i];
}
printf("\n");
}
void displayAll(KQueue *q)
{
for (int i = 0; i < q->k; i++)
{
display(q, i);
}
}
void printKQueue(KQueue *q)
{
printf("Array: ");
printf("[");
for (int i = 0; i < q->n; i++)
{
printf("%d ", q->arr[i]);
if (i < q->n - 1)
printf(", ");
}
printf("]\n");
printf("Front: ");
printf("[");
for (int i = 0; i < q->k; i++)
{
printf("%d ", q->front[i]);
if (i < q->k - 1)
printf(", ");
}
printf("]\n");
printf("Rear: ");
printf("[");
for (int i = 0; i < q->k; i++)
{
printf("%d ", q->rear[i]);
if (i < q->k - 1)
printf(", ");
}
printf("]\n");
printf("Next: ");
printf("[");
for (int i = 0; i < q->n; i++)
{
printf("%d ", q->next[i]);
if (i < q->n - 1)
printf(", ");
}
printf("]\n");
printf("Free: %d\n", q->free);
}
int main()
{
int k, n, choice, qno, val;
printf("Enter the number of queues: ");
scanf("%d", &k);
printf("Enter the size of the array: ");
scanf("%d", &n);
KQueue *q = createKQueue(k, n);
while (1)
{
printKQueue(q);
printf("\nMenu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display a Queue\n");
printf("4. Display All Queues\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter queue number (0 to %d): ", k - 1);
scanf("%d", &qno);
printf("Enter value to enqueue: ");
scanf("%d", &val);
enqueue(q, val, qno);
break;
case 2:
printf("Enter queue number (0 to %d): ", k - 1);
scanf("%d", &qno);
dequeue(q, qno);
break;
case 3:
printf("Enter queue number (0 to %d): ", k - 1);
scanf("%d", &qno);
display(q, qno);
break;
case 4:
displayAll(q);
break;
case 5:
printf("Exiting...\n");
free(q->arr);
free(q->front);
free(q->rear);
free(q->next);
free(q);
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}