forked from mohitarora3/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcircular_queue.cpp
More file actions
219 lines (165 loc) · 3.08 KB
/
circular_queue.cpp
File metadata and controls
219 lines (165 loc) · 3.08 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include<conio.h>
#include <cstdlib>
#define SIZE 10
using namespace std;
class queue
/*
objective: Create a class to implement Queue(circular) using dynamically created array
input parameters: none
output value: none
description: Class definition
approach: Class definition provides data member and member functions for the Queue class
*/
{
int *arr; // array to store queue elements
int capacity; // maximum capacity of the Q
int front; // front points to front element in the Q
int rear; // rear points to last element in the Q
int count; // current size of the Q
public:
queue(int size = SIZE) // constructor
{
arr=new int[size];
capacity=size;
front=-1;
rear=-1;
count=0;
}
~queue()
{
delete []arr;
}
int dequeue();
void enqueue(int);
int peek(); // returns front element
int size(); // returns current size of Q
bool isEmpty();
bool isFull();
};
void queue::enqueue(int ele)
{
if((front==0&&rear==capacity-1)||front==rear+1)
{
cout<<"OVERFLOW";
return;
}
else if(rear==-1)
{
rear=front=0;
arr[rear]=ele;
++count;
}
else {
rear=(rear+1)%capacity;
arr[rear]=ele;
++count;
}
}
int queue::dequeue()
{
int x;
if(front==-1&&rear==-1)
{
return -1;
}
else
{
--count;
x=arr[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else front=(front+1)%capacity;
return x;
}
}
int queue::peek()
{
if(count==0)
{
return -1;
}
return arr[front];
}
int queue::size()
{
return count;
}
bool queue::isEmpty()
{
return(count==0);
}
bool queue::isFull()
{
return (count==capacity);
}
int main()
{
/*
objective:to implement queue operations
*/
int ch,n,ele,x;
bool ans;
char c;
cout<<"Enter size";
cin>>n;
queue q(n);
do
{
cout<<"STACK IMPLEMENTATION::";
cout<<"\n1.ENQUE";
cout<<"\n2.DEQUE";
cout<<"\n3.TOPMOST ELEMENT";
cout<<"\n4.CURRENT SIZE";
cout<<"\n5.IS QUEUE EMPTY?";
cout<<"\n6.IS QUEUE FULL?";
cout<<"\n\tenter your choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter element";
cin>>ele;
q.enqueue(ele);
break;
case 2:x=q.dequeue();
if(x==-1)
{
cout<<"\nUNDERFLOW!";
}
else
cout<<"\nPOPPED ELEMENT IS: "<<x;
break;
case 3:x=q.peek();
if(x==-1)
{
cout<<"\nUNDERFLOW!";
}
else
cout<<"\nPEEKED ELEMENT IS: "<<x;
break;
case 4:x=q.size();
cout<<"\nCURRENT SIZE is: "<<x;
break;
case 5:ans=q.isEmpty();
if(ans)
cout<<"\nSTACK IS EMPTY";
else
cout<<"\nSTACK IS NOT EMPTY";
break;
case 6:ans=q.isFull();
if(ans)
cout<<"\nSTACK IS FULL";
else
cout<<"\nSTACK IS NOT FULL";
break;
default:cout<<"\nWRONG CHOICE!!";
exit(0);
}
cout<<"\nwant to continue (enter Y or Y) ";
cin>>c;
}while(c=='y'||c=='Y');
getch();
}