forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Queue_using_Array.cpp
More file actions
204 lines (172 loc) · 3.58 KB
/
Circular_Queue_using_Array.cpp
File metadata and controls
204 lines (172 loc) · 3.58 KB
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
/*
Author: codewithmini
*/
/* Problem Statement :
Implement Circular Queue using Array. Perform following operations on it.
a) Insertion (Enqueue)
b) Deletion (Dequeue)
c) Display
(Note: Handle queue full condition by considering a fixed size of a queue.) */
#include<iostream>
using namespace std;
// Opetations on Queue required
struct Queue
{
private:
int rear,front,size;
public:
Queue(int n)
{
size=n;
front=-1;
rear=-1;
}
void enqueue(int a[]);
void dequeue(int a[]);
void show(int a[]);
};
void Queue::enqueue(int a[]) //INSERTION IN QUEUE
{
if((rear+1)%size == front)
{
cout<<"\n QUEUE FULL!!\n";
}
else
{
int x;
cout<<"\n ENTER THE ELEMENT TO BE INSERTED: ";
cin>>x;
if(rear ==-1)
{
front=0;
rear=0;
}
else
{
rear=(rear+1)%size;
}
a[rear]=x;
}
}
void Queue::dequeue(int a[]) //DELETION FROM QUEUE
{
if(rear == -1 && front == -1)
{
cout<<"\n EMPTY QUEUE! (nothing to delete) \n ";
return;
}
else
{
cout<<"Deleted element from queue is "<<a[front]<<"\n";
if(rear == front)
{
rear= -1;
front= -1;
}
else
{
front=(front+1)%size;
}
}
}
void Queue::show(int a[]) //DISPLAY OF ELEMENTS
{
if(front == -1)
{
cout<<"\n QUEUE IS EMPTY! (nothing to show )) \n";
return;
}
else
{
int x=front;
while(x != rear)
{
cout<<a[x]<<"\t";
x=(x+1)%size;
}
cout<<a[rear]<<"\t";
}
}
int main() //main function
{
int n,ch;
cout<<"Enter the size of the array: ";
cin>>n;
int a[n];
Queue s(n);
cout<<" \n [1] INSERTION ";
cout<<" \n [2] DELETION ";
cout<<" \n [3] DISPLAY \n";
do
{
cout<<" \n *****Enter your choice***** (want to exit? enter 0) \n";
cin>>ch;
switch(ch)
{
case 1:
{
s.enqueue(a);
break;
}
case 2:
{
s.dequeue(a);
break;
}
case 3:
{
s.show(a);
break;
}
}
}
while(ch>0 && ch<4);
cout<<"\n \t OPERATION COMPLETED!! THANK YOU :) \t \n";
return 0;
}
/*
--------------------------------------------------------------------------------------------------------------------------
OUTPUT :
Enter the size of the array: 6
[1] INSERTION
[2] DELETION
[3] DISPLAY
*****Enter your choice***** (want to exit? enter 0)
1
ENTER THE ELEMENT TO BE INSERTED: 2
*****Enter your choice***** (want to exit? enter 0)
1
ENTER THE ELEMENT TO BE INSERTED: 6
*****Enter your choice***** (want to exit? enter 0)
1
ENTER THE ELEMENT TO BE INSERTED: 8
*****Enter your choice***** (want to exit? enter 0)
1
ENTER THE ELEMENT TO BE INSERTED: 9
*****Enter your choice***** (want to exit? enter 0)
1
ENTER THE ELEMENT TO BE INSERTED: 22
*****Enter your choice***** (want to exit? enter 0)
3
2 6 8 9 22
*****Enter your choice***** (want to exit? enter 0)
1
ENTER THE ELEMENT TO BE INSERTED: 7
*****Enter your choice***** (want to exit? enter 0)
1
QUEUE FULL!!
*****Enter your choice***** (want to exit? enter 0)
2
Deleted element from queue is 2
*****Enter your choice***** (want to exit? enter 0)
2
Deleted element from queue is 6
*****Enter your choice***** (want to exit? enter 0)
3
8 9 22 7
*****Enter your choice***** (want to exit? enter 0)
0
OPERATION COMPLETED!! THANK YOU :)
...Program finished with exit code 0
Press ENTER to exit console.
*/