-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularQueue.cpp
231 lines (200 loc) · 4.54 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
230
231
/**
* Author: Hemant Tripathi
*/
#include <iostream>
using namespace std;
#define MAXQSIZE 5
class CircularQueue {
int front, rare;
public:
int cqueue[MAXQSIZE];
CircularQueue() {
front = rare = MAXQSIZE - 1;
}
bool insertOp(int element);
bool removeOp();
int getFirstElement();
int getFirstElementPosition();
int getLastElement();
int getLastElementPosition();
bool isEmpty();
bool isOverflow();
void showElements();
int totalCount();
};
bool CircularQueue::insertOp(int element) {
if(isOverflow()) {
cout << "Queue is overflow! Cannot insert.";
return false;
}
if(rare == MAXQSIZE-1 && (front != 0)) {
rare = 0;
} else {
rare++;
}
cqueue[rare] = element;
cout << "element " << element << " inserted at position " << rare << endl;
return true;
}
bool CircularQueue::removeOp() {
if(isEmpty()) {
cout << "cqueue is empty" << endl;
return false;
}
if(front == MAXQSIZE-1) {
front = 0;
} else {
front++;
}
cout << "popped item " << cqueue[front] << " from the queue" << endl;
return true;
}
int CircularQueue::getFirstElement() {
if(isEmpty()) {
cout << "Empty Queue!" << endl;
return -1;
}
if(front == MAXQSIZE-1) {
return cqueue[0];
} else {
return cqueue[front+1];
}
}
int CircularQueue::getFirstElementPosition() {
if(isEmpty()) {
return -1;
}
if(front == MAXQSIZE-1) {
return 0;
} else {
return front+1;
}
}
int CircularQueue::getLastElement() {
if(isEmpty()) {
cout << "Empty Queue!" << endl;
return -1;
}
return cqueue[rare];
}
int CircularQueue::getLastElementPosition() {
if(isEmpty()) {
return -1;
}
return rare;
}
bool CircularQueue::isEmpty() {
if(front == rare) {
return true;
} else {
return false;
}
}
bool CircularQueue::isOverflow() {
if((front-rare == 1) || (rare-front == MAXQSIZE-1)) {
return true;
} else {
return false;
}
}
void CircularQueue::showElements() {
if(rare < front) {
for (int i = front+1; i < MAXQSIZE; i++) {
cout << "Element " << cqueue[i] << " at position " << i << endl;
}
for (int i = 0; i <= rare; i++) {
cout << "Element " << cqueue[i] << " at position " << i << endl;
}
} else if(front < rare) {
for(int i = front+1; i <= rare; i++) {
cout << "Element " << cqueue[i] << " at position " << i << endl;
}
} else {
cout << "No Element Found! Empty stack.";
}
}
int CircularQueue::totalCount() {
if(rare < front) {
return MAXQSIZE - front + rare;
} else if(front < rare) {
return rare - front;
} else {
return 0;
}
}
int main() {
class CircularQueue cQueue;
bool exit = false;
cout << "########## Starting Circular Queue Operation ###########";
while(1) {
if(exit) {
cout << "Aborting the program" << endl;
break;
}
cout << "Select an Option: " << endl;
cout << "1. Insert An Element" << endl;
cout << "2. Remove and Element" << endl;
cout << "3. Get First Element of Queue" << endl;
cout << "4. Get First Element Position in Queue" << endl;
cout << "5. Get Last Element of Queue" << endl;
cout << "6. Get Last Element Position in Queue" << endl;
cout << "7. Get Total Number of Element In Queue" << endl;
cout << "8. Show all element of queue" << endl;
cout << "9. Exit" << endl;
int choice = -1;
cin >> choice;
switch(choice) {
case 1:
int element;
cout << "Please enter an element to insert into the queue" << endl;
cin >> element;
cQueue.insertOp(element);
break;
case 2:
cQueue.removeOp();
break;
case 3:
int firstElement;
firstElement = cQueue.getFirstElement();
if(firstElement >= 0) {
cout << "First Element of Queue is: " << firstElement << endl;
} else {
cout << "No Element Found!" << endl;
}
break;
case 4:
cout << "First Element Position is: "<< cQueue.getFirstElementPosition() << endl;
break;
case 5:
int lastElement;
lastElement = cQueue.getLastElement();
if(lastElement >= 0) {
cout << "Last Element of Queue is: " << lastElement << endl;
} else {
cout << "No Element Found!" << endl;
}
break;
case 6:
cout << "Last Element Position is: " << cQueue.getLastElementPosition() << endl;
break;
case 7:
if(cQueue.isEmpty()) {
cout << "Empty Queue! Zero Element found!" << endl;
} else {
cout << "Total Element in Queue = " << cQueue.totalCount() << endl;
}
break;
case 8:
if(cQueue.isEmpty()) {
cout << "No Element found in the queue" << endl;
} else {
cQueue.showElements();
}
break;
case 9:
exit = true;
break;
}
}
return 0;
}