-
Notifications
You must be signed in to change notification settings - Fork 2
/
circular_array_queue.c
116 lines (88 loc) · 1.79 KB
/
circular_array_queue.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
#include<stdio.h>
#include<stdlib.h>
#define size 5
int queue[size];
int front = -1, rear = -1;
int isEmpty() {
return (front == -1 && rear == -1);
}
int isFull() {
return ((rear+1)%size == front);
}
void enqueue(int item) {
if(isEmpty()) {
front = rear = 0;
queue[rear] = item;
}
else if(isFull()) printf("Queue is Full!\n");
else {
rear = (rear+1)%size;
queue[rear] = item;
}
}
void dequeue() {
if(isEmpty()) printf("Nothing to dequeue...\n");
else if(front == rear) {
front = rear = -1;
}
else {
int temp = front;
printf("Dequeued Element is : %d\n", queue[temp]);
front = (front+1)%size;
}
}
void display() {
if(isEmpty()) printf("Nothing in the queue\n");
else {
int i = front;
printf("Queue is : ");
while(i != rear) {
printf("%d ", queue[i]);
i = (i+1)%size;
}
printf("%d", queue[rear]);
printf("\n");
}
}
void peek() {
if(isEmpty()) printf("Nothing in the queue\n");
else printf("Front value of the queue : %d\n", queue[front]);
}
int main() {
enqueue(5);
enqueue(4);
enqueue(3);
display();
dequeue();
display();
enqueue(6);
dequeue();
dequeue();
enqueue(7);
display();
peek();
dequeue();
dequeue();
display();
dequeue();
enqueue(100);
enqueue(1000);
dequeue();
display();
return 0;
}
/*
Output:
Queue is : 5 4 3
Dequeued Element is : 5
Queue is : 4 3
Dequeued Element is : 4
Dequeued Element is : 3
Queue is : 6 7
Front value of the queue : 6
Dequeued Element is : 6
Nothing in the queue
Nothing to dequeue...
Dequeued Element is : 100
Queue is : 1000
*/