-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_queue_array.c
98 lines (85 loc) · 1.99 KB
/
circular_queue_array.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
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
struct Queue {
int arr[MAX];
int front, rear;
};
void initializeQueue(struct Queue *q) {
q->front = -1;
q->rear = -1;
}
int isFull(struct Queue *q) {
return ((q->rear + 1) % MAX == q->front);
}
int isEmpty(struct Queue *q) {
return (q->front == -1);
}
void enqueue(struct Queue *q, int data) {
if (isFull(q)) {
printf("Queue is full! Cannot enqueue %d\n", data);
return;
}
if (isEmpty(q)) {
q->front = 0;
}
q->rear = (q->rear + 1) % MAX;
q->arr[q->rear] = data;
printf("Enqueued: %d\n", data);
}
void dequeue(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty! Cannot dequeue.\n");
return;
}
int dequeuedValue = q->arr[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
}
else {
q->front = (q->front + 1) % MAX;
}
printf("Dequeued: %d\n", dequeuedValue);
}
void display(struct Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty!\n");
return;
}
printf("Queue: ");
int i = q->front;
while (1) {
printf("%d ", q->arr[i]);
if (i == q->rear) break;
i = (i + 1) % MAX;
}
}
int main() {
struct Queue q;
initializeQueue(&q);
int choice, data;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to enqueue: ");
scanf("%d", &data);
enqueue(&q, data);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Enter a valid choice!\n");
}
} while (choice != 4);
return 0;
}