-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathCircular_Queue.cpp
258 lines (231 loc) · 6.87 KB
/
Circular_Queue.cpp
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
250
251
252
253
254
255
256
257
258
/*
This is a code of circular queue data structure. It is a linear data structure, which obeys the principal
of First in First Out(FIFO). Circular queue data structure is a updated version of linear queue data structure,
which have some advantages over linear queue. The following code shows the implementation of circular queue data structure,
in a very clean and understandable way.
*/
#include<iostream>
#include<stdlib.h>
// Defining max size of the queue as 5
#define max 5
using namespace std;
// Writing a class where all the basic functions of the circular queue presides
class CircularQueue
{
/*
Defining all the variables as public variables.
(It is not necessory of define the variables as public,you can define the variables as private also)*/
public:
int front;
int rear;
int arr[max];
// Initializing a default constructor of the class
CircularQueue(){
front = -1;
rear = -1;
for(int i = 0; i < max; i++){
arr[i] = 0;
}
}
// Defining the isFull function to check whether the queue is full or not
bool isFull(){
if ((rear + 1) % max == front)
return true;
else
return false;
}
// Defining the isEmpty function to check whether the queue is empty or not
bool isEmpty(){
if (front == -1 && rear == -1)
return true;
else
return false;
}
// Defining the enqueue function, to insert an item in the queue
void enqueue(int item){
if(isFull())
{
cout << "Queue is full" << endl;
}
else if (isEmpty()){
front=rear=0;
arr[rear] = item;
}
else {
rear = (rear + 1) % 5;
arr[rear] = item;
}
}
// Defining the dequeue function, to delete item from the queue
int dequeue(){
int x = 0;
if(front == rear){
x = arr[front];
front = rear = -1;
return x;
}
else
{
x = arr[front];
arr[front] = 0;
front = (front + 1) % 5;
return x;
}
}
// Defing display function to display the whole queue
void display(){
for (int i = 0; i < 5; i++)
{
cout << arr[i] <<endl;
}
}
};
// Testing the above functions:
int main()
{
// Declaring an object to the class
CircularQueue Q1;
// A menu driven program to check all the defined functions in the class
while (1)
{
cout << "1. Insert an element in the queue - enqueue" << endl;
cout << "2. Delete element from the queue - Dequeue" << endl;
cout << "3. Display items in the queue" << endl;
cout << "4. To check whether the queue is empty or not" << endl;
cout << "5. To check whether the queue is full or not" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice" << endl;
int n;
cin >> n;
switch(n){
case 1:
int item;
cout << "Enter the item" << endl;
cin >> item;
Q1.enqueue(item);
break;
case 2:
if(!Q1.isEmpty()){
cout << "The dequeued value is: " << Q1.dequeue() << endl;
}
else{
cout << "The queue is empty" << endl;
}
break;
case 3:
cout << "The Queue is" << endl;
Q1.display();
break;
case 4:
if(Q1.isEmpty()){
cout<<"Queue is Empty"<<endl;
}
else{
cout<<"Queue is not Empty"<<endl;
}
break;
case 5:
if(Q1.isFull()){
cout<<"Queue is Full"<<endl;
}
else{
cout<<"Queue is not Full"<<endl;
}
break;
case 6:
exit(1);
default:
cout << "Choose the options correctly";
break;
}
}
return 0;
}
/*
Sample Input/Output:
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
1
Enter the item
10
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
1
Enter the item
20
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
3
The Queue is
10
20
0
0
0
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
2
The dequeued value is: 10
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
2
The dequeued value is: 20
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
4
Queue is Empty
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
5
Queue is not Full
1. Insert an element in the queue - enqueue
2. Delete element from the queue - Dequeue
3. Display items in the queue
4. To check whether the queue is empty or not
5. To check whether the queue is full or not
6. Exit
Enter your choice
6
*/
/*
The time complexities of different operations in a circular queue data structure are:-
1) The time complexity of enqueue operation is O(1), that is constant time.
2) The time complexity of dequeue operation is O(1), that is constant time.
3) The time complexity for checking whether the queue is empty of full is O(1), that is constant time.
4) The time complexity of display function is O(n), where n is the number of Elements in the queue.
*/