-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularQueue.c
79 lines (70 loc) · 1.51 KB
/
circularQueue.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
#include <stdio.h>
#include <stdlib.h>
#define size 100
int q[size];
int f=-1, r = -1;
void enqueue(int item);
int dequeue();
void display();
int main() {
int choice, item;
while (1) {
printf("Enter your choice:\n 1. enqueue\n 2. dequeue\n 3. display\n 4. exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter item to be added to queue: ");
scanf("%d", &item);
enqueue(item);
break;
case 2:
printf("Element deleted: %d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
void enqueue(int item) {
if ((r + 1) % size == f) {
printf("Queue is full\n");
} else {
r = (r + 1) % size;
q[r] = item;
if (f == -1) {
f = 0;
}
}
}
int dequeue() {
int temp;
if (f == -1) {
printf("Queue is empty\n");
return -1;
} else if (f == r) {
temp = q[f];
f = -1;
r = -1;
} else {
temp = q[f];
f = (f + 1) % size;
}
return temp;
}
void display() {
int i;
if (f == -1) {
printf("Queue empty\n");
} else {
for (i = f; i != (r + 1) % size; i = (i + 1) % size) {
printf("%d ", q[i]);
}
printf("\n");
}
}