-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularQueue.cpp
92 lines (74 loc) · 1.65 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
#include<iostream>
#include<vector>
using namespace std;
class CircularQueue {
private:
int front, rear, maxSize;
vector<int> queElements;
public:
CircularQueue(int size) {
this->front = this->rear = -1;
this->maxSize = size;
this->queElements.reserve(size);
}
void enQueue(int value) {
if (this->isFull()) {
cout << "queue is overflow";
} else {
this->rear = (this->rear + 1) % this->maxSize;
this->queElements[this->rear] = value;
if (this->front == -1)
this->front = this->rear;
}
}
int deQueue() {
if (this->isEmpty()) {
cout << "Queue is Empty";
return 0;
} else {
int data = this->queElements[this->front];
if (this->front == this->rear)
this->front = this->rear = -1;
else
this->front = (this->front + 1) % this->maxSize;
return data;
}
}
int size() {
return (this->maxSize - this->front + this->rear + 1) % this->maxSize;
}
bool isEmpty() {
return (this->front == -1);
}
bool isFull() {
return ((this->rear + 1) % this->maxSize == this->front);
}
};
int main() {
CircularQueue theQueue(10);
//lets play with queue
//arrange some elements
theQueue.enQueue(10);
theQueue.enQueue(20);
theQueue.enQueue(30);
theQueue.enQueue(40);
theQueue.enQueue(50);
theQueue.enQueue(60);
//remove some elements
theQueue.deQueue();
theQueue.deQueue();
theQueue.deQueue();
//add some elements
theQueue.enQueue(70);
theQueue.enQueue(80);
theQueue.enQueue(90);
theQueue.enQueue(100);
theQueue.enQueue(110);
theQueue.enQueue(120);
cout << "size Of Queue :" << theQueue.size() << endl;
while (!theQueue.isEmpty()) {
int de = theQueue.deQueue();
cout << de << endl;
}
return 0;
}