-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
queue_using_circular_array.cpp
224 lines (180 loc) · 4.35 KB
/
queue_using_circular_array.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
/*
Queue is a linear data structure which follows FIFO
(First In First Out) Rule. It can be implemented using array/
linked list/ stacks.
*/
#include<bits/stdc++.h>
using namespace std;
//We are creating a class for array
class Queue
{
private:
int front;
int rear;
int capacity;
int *ptr;
public:
//Constructor to initialize the data members of Queue class
Queue(int cap)
{
front = 0;
rear = 0;
capacity = cap;
ptr = new int [capacity];
}
//Checking if the Queue is empty or not
bool isEmpty()
{
if(front == rear)
return true;
return false;
}
//Checking if the Queue is full or not
bool isFull()
{
if((rear+1) % capacity == front)
return true;
return false;
}
//Inserting data into the queue
void enQueue()
{
if(isFull())
cout<<"\nQueue is Full"<<endl;
else
{
int data;
cout<<"\nEnter data to be inserted : ";
cin>>data;
rear = (rear+1)%capacity;
ptr[rear] = data;
}
}
//Deleting data from the queue
void deQueue()
{
if(isEmpty())
cout<<"\nQueue is Empty"<<endl;
else
{
int data;
front = (front+1)%capacity;
data = ptr[front];
cout<<endl<<data<<" is deleted from Queue"<<endl;
}
}
//Traversing queue and printing data
void display()
{
int i;
if(rear == front)
cout<<"\nQueue is Empty"<<endl;
else
{
cout<<"Queue is : ";
for(i = front+1 ; i != rear ; i = (i+1) % capacity)
cout<<ptr[i]<<" ";
cout<<ptr[rear]<<endl;
}
}
};
//Driver Program
int main()
{
int cap,choice;
cout<<"Enter the size or capacity of Queue : ";
cin>>cap;
Queue q(cap);
while(1)
{
cout<<endl;
cout<<"1. To enter data into queue."<<endl;
cout<<"2. To delete data from queue."<<endl;
cout<<"3. To display the data."<<endl;
cout<<"4. TO EXIT."<<endl;
cout<<"\nENTER YOUR CHOICE... : ";
cin>>choice;
switch(choice)
{
case 1:
q.enQueue();
break;
case 2:
q.deQueue();
break;
case 3:
q.display();
break;
case 4:
exit(0);
break;
default:
cout<<"\nINVALID CHOICE..."<<endl;
}
}
return 0;
}
/*
Time Complexities:
Enqueue --> O(1)
DeQueue --> O(1)
IsEmpty --> O(1)
IsFull --> O(1)
Space Complexity: O(n) [for array]
----------------------------------
Sample Input - Output :
Enter the size or capacity of Queue : 5
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 1
Enter data to be inserted : 10
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 1
Enter data to be inserted : 20
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 1
Enter data to be inserted : 30
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 1
Enter data to be inserted : 40
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 3
Queue is : 10 20 30 40
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 2
10 is deleted from Queue
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 1
Enter data to be inserted : 50
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 3
Queue is : 20 30 40 50
1. To enter data into queue.
2. To delete data from queue.
3. To display the data.
4. TO EXIT.
ENTER YOUR CHOICE... : 4
*/