-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
350 lines (305 loc) · 5.89 KB
/
Copy pathQueue.cpp
File metadata and controls
350 lines (305 loc) · 5.89 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <iostream>
#include <cstring>
using namespace std;
class IQueue {
public:
virtual ~IQueue() = default;
virtual void Clear() = 0;
virtual bool IsEmpty() = 0;
virtual bool IsFull() = 0;
virtual void Enqueue(int value) = 0;
virtual int Dequeue() = 0;
virtual int GetFirstElement() = 0;
};
/*
// Function displaying the elements of Circular Queue
void displayQueue(struct Queue *q)
{
struct Node *temp = q->front;
printf("\nElements in Circular Queue are: ");
while (temp->link != q->front)
{
printf("%d ", temp->data);
temp = temp->link;
}
printf("%d", temp->data);
}*/
class CircularList : public IQueue {
private:
struct Node {
int data;
struct Node* link;
};
struct Node *first, *last;
public:
CircularList() {
first = last = NULL;
}
~CircularList() {
Clear();
}
bool IsEmpty() {
return ((first == NULL));
}
bool IsFull() {
return 0;
}
void Clear() {
// delete all the nodes alloted
struct Node *temp = first;
while ((temp) && (first != last)) {
first = first->link;
last->link = first;
free(temp);
temp = first;
}
free(temp);
first = last = NULL;
}
void Enqueue(int value);
int Dequeue();
int GetFirstElement();
};
class CircularQueue : public IQueue {
public:
CircularQueue(int s) {
ind_first = ind_last = -1;
contents = new int(size);
size = s;
}
~CircularQueue() {
delete contents;
}
bool IsEmpty() {
return ((ind_first == -1));
}
bool IsFull() {
return (ind_first == (ind_last + 1) % size);
}
void Clear() {
ind_first = ind_last = -1;
memset(contents, 0, size);
}
void Enqueue(int value);
int Dequeue();
int GetFirstElement();
private:
int ind_first, ind_last, size;
int *contents;
};
class ArrayQueue : public IQueue {
public:
ArrayQueue(int s) {
ind_first = ind_last = -1;
contents = new int(size);
size = s;
}
~ArrayQueue() {
delete contents;
}
bool IsEmpty() {
return ind_first == -1;
}
bool IsFull() {
return ((ind_first == 0) && (ind_last == size - 1) || (ind_first == ind_last + 1));
}
void Clear() {
ind_first = ind_last = -1;
memset(contents, 0, size);
}
void Enqueue(int value);
int Dequeue();
int GetFirstElement();
private:
int ind_first, ind_last, size;
int *contents;
};
void ArrayQueue::Enqueue(int value) {
if (!IsFull()) {
if (ind_last == size - 1 || ind_last == -1) {
contents[0] = value;
ind_last = 0;
if (ind_first == -1)
ind_first = 0;
}
else {
contents[++ind_last] = value;
}
}
}
int ArrayQueue::Dequeue() {
int tmp = contents[ind_first];
if (ind_first == ind_last) {
ind_last = ind_first = -1;
}
else if (ind_first == size - 1) {
ind_first = 0;
}
else {
ind_first++;
}
return tmp;
}
int ArrayQueue::GetFirstElement() {
if (!IsEmpty())
return contents[ind_first];
return -1;
}
void CircularQueue::Enqueue(int value) {
if (!IsFull()) {
if (ind_first == -1) {
ind_first = 0;
ind_last = 0;
}
else {
if (ind_last == size - 1)
ind_last = 0;
else
ind_last++;
}
contents[ind_last] = value;
}
}
int CircularQueue::Dequeue() {
if (IsEmpty()) return -1;
int tmp = contents[ind_first];
if (ind_first == ind_last) {
ind_first = ind_last = -1;
}
else {
if (ind_first == size - 1) {
ind_first = 0;
}
else
ind_first++;
}
return tmp;
}
int CircularQueue::GetFirstElement() {
if (!IsEmpty())
return contents[ind_first];
return -1;
}
// Function to create Circular queue
void CircularList::Enqueue(int value)
{
struct Node *temp = new struct Node;
temp->data = value;
if (first == NULL)
first = temp;
else
last->link = temp;
last = temp;
last->link = first;
}
// Function to delete element from Circular Queue
int CircularList::Dequeue() {
if (IsEmpty())
return -1;
// If this is the last node to be deleted
int value; // Value to be dequeued
if (first == last) {
value = first->data;
delete first;
first = NULL;
last = NULL;
}
else {
// There are more than one nodes
struct Node *temp = first;
value = temp->data;
first = first->link;
last->link = first;
free(temp);
}
return value;
}
int CircularList::GetFirstElement() {
if (!IsEmpty())
return first->data;
return -1;
}
int main() {
// Choose the data structure to be used for the implementation
int option = 1;
IQueue *queue = NULL;
cout << "Choose the Desired Data Type for Queue Implementation" << endl;
cout << "1. Circular List " << endl;
cout << "2. Circular Array" << endl;
cout << "3. Regular Array" << endl;
cout << "0. Exit" << endl;
cout << "Please input your option:";
cin >> option;
switch (option) {
case 0:
break;
case 1: {
CircularList *q = new CircularList();
queue = q;
}
break;
case 2: {
CircularQueue *q = new CircularQueue(5);
queue = q;
}
break;
case 3: {
ArrayQueue *q = new ArrayQueue(5);
queue = q;
}
break;
default:
cout << "Select a valid option";
option = 0;
break;
}
while (option && queue != NULL) {
cout << "Choose the Desired operation on the Queue" << endl;
cout << "1. Insert " << endl;
cout << "2. Delete" << endl;
cout << "3. Clear" << endl;
cout << "4.Get First Item" << endl;
cout << "0. Exit" << endl;
cout << "Please input your option:";
cin >> option;
int value;
switch (option) {
case 0:
break;
case 1: {
cout << "enter the value to insert: " << endl;
cin >> value;
queue->Enqueue(value);
}
break;
case 2: {
cout << "The Deleted Item is " << queue->Dequeue() << endl;
}
break;
case 3: {
queue->Clear();
cout << "The Queue is Cleared" << endl;
}
break;
case 4: {
cout << "The First Item is :" << queue->GetFirstElement() << endl;
}
break;
default:
cout << "Select a valid option";
option = 5;
break;
}
}
queue->Enqueue(30);
queue->Enqueue(40);
queue->Enqueue(50);
queue->Enqueue(60);
queue->Enqueue(70);
cout << queue->Dequeue();
cout << queue->Dequeue();
cout << queue->Dequeue();
cout << queue->Dequeue();
cout << queue->Dequeue();
delete queue;
}