-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular queue.cpp
82 lines (70 loc) · 1.75 KB
/
circular queue.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
#include <iostream>
#include <queue>
using namespace std;
// Circular Queue Class
class CircularQueue {
public:
int size;
queue<int> q;
CircularQueue(int k) {
size = k;
}
// Enqueue Operation
bool enqueue(int val) {
if (q.size() < size) {
q.push(val);
return true;
}
return false;
}
// Dequeue Operation
bool dequeue() {
if (!q.empty()) {
q.pop();
return true;
}
return false;
}
// Front Operation
int front() {
if (!q.empty()) {
return q.front();
}
return -1;
}
// Rear Operation
int rear() {
if (!q.empty()) {
return q.back();
}
return -1;
}
// Is Empty Operation
bool isEmpty() {
return q.empty();
}
// Is Full Operation
bool isFull() {
return q.size() == size;
}
};
int main() {
CircularQueue cq(3);
cout << cq.enqueue(1) << endl; // true
cout << cq.enqueue(2) << endl; // true
cout << cq.enqueue(3) << endl; // true
cout << cq.enqueue(4) << endl; // false
cout << cq.front() << endl; // 1
cout << cq.rear() << endl; // 3
cout << cq.dequeue() << endl; // true
cout << cq.enqueue(4) << endl; // true
cout << cq.front() << endl; // 2
cout << cq.rear() << endl; // 4
cout << cq.dequeue() << endl; // true
cout << cq.dequeue() << endl; // true
cout << cq.dequeue() << endl; // true
cout << cq.dequeue() << endl; // false
cout << cq.isEmpty() << endl; // true
cout << cq.isFull() << endl; // false
return 0;
}