-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.cpp
229 lines (203 loc) · 4.61 KB
/
circularqueue.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
#include <iostream>
#include <cstdlib>
#define SIZE 10
using namespace std;
template <class T>
class circularqueue
/*
objective: Create a class for implementing circular queue using array
input parameters:
front -> front index of a circular queue
rear -> last or a top element of circular queue
capacity-> size of circular queue
arr -> pointer to array
output -> templete of a circular queue of maximum size 100
Description:
Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out)
principle and the last position is connected back to the first position to make a circle. It is also called 'Ring Buffer
approach: Class defines data member and member function of the queue class
*/
{
T *arr; // array to store queue elements
int capacity; // maximum capacity of the circular Q
int front; // front points to front element in the circular Q
int rear; // rear points to last element in the circular Q
int count; // current size of the circular Q
public:
circularqueue(T size = SIZE) // constructor
{
arr = new int [size]; //dynamicly allocate a size to array
capacity=size;
front=-1;
rear=-1;
}
~circularqueue()
{
delete arr; // deconstructor
}
void dequeue()
{
if(isEmpty()==true)
{
cout<<"Circular Queue is Empty!";
}
else if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=(front+1)%capacity;
}
}
void enqueue(T &x)
{
if(isFull()==true) //checking whether circular Queue is full or not!
{
cout<<"Queue Is Full!"<<endl;
}
else
{
if(front==-1 && rear ==-1)
{
front = front+1;
rear = rear+1;
arr[rear]=x;
}
else
{
rear = (rear+1)%capacity;
arr[rear]=x;
}
}
}
int peek() // returns front element
{
return(arr[rear]);
}
int size() // returns current size of circular Queue
{
return((rear+1)%capacity);
}
bool isEmpty()
{
if(front==-1 && rear==-1)
{
return true;
}
else
{
return false;
}
}
bool isFull() //checking whether circular Queue is full or not!
{
if((rear+1)%capacity==front)
{
return true;
}
else
{
return false;
}
}
void show() // To Display the circular queue
{
int temp; // temprorily store the value of the circular queue
temp=front;
if(isEmpty()==true)
{
cout<<"Queue is empty";
}
else
{
while(rear!=temp)
{
cout<<arr[temp]<<"\t"; //Displying element of the circular Queue
temp=temp+1;
}
cout<<arr[temp]<<endl;
}
}
};
int main()
{
char choice1='y';
circularqueue<int> s; // Creating object of Queue
cout<<"---Operation on Stack-----"<<endl;
cout<<"1) Enqueue"<<endl;
cout<<"2) Dequeue"<<endl;
cout<<"3) Peek element to Queue"<<endl;
cout<<"4) Size to Queue"<<endl;
cout<<"5) check whether Queue is empty or not"<<endl;
cout<<"6) check whether Queue is full or not"<<endl;
cout<<"7) print a Queue"<<endl;
int choice;
do
{
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
int element;
cout<<"Enter the element to Enqueue a Queue "<<endl;
cin>>element;
s.enqueue(element);
break;
case 2:
if(s.isEmpty()==true)
{
cout<<"Queue is empty"<<endl;
}
else
{
s.dequeue();
}
break;
case 3:
element=s.peek();
cout<<"top of the Queue : "<<element<<endl;
break;
case 4:
cout<<"size is : "<<s.size()<<endl;
break;
case 5:
if(s.isEmpty()==true)
{
cout<<"TRUE Queue is empty"<<endl;
}
else
{
cout<<"false Queue is not empty"<<endl;
}
break;
case 6:
if(s.isFull()==true)
{
cout<<"TRUE Queue is full"<<endl;
}
else
{
cout<<"false Queue is not full"<<endl;
}
break;
case 7:
if(s.isEmpty()==true)
{
cout<<"Queue is empty!";
}
else
{
cout<<"element in Queue is"<<endl;
s.show();
}
break;
default:
cout<<"enter the right choice";
}
cout<<"Press y for continue"<<endl;
cin>>choice1;
}while(choice1=='y'||choice1=='Y');
}