-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path622-Design-Circular-Queue.cpp
65 lines (58 loc) · 1.28 KB
/
622-Design-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
class MyCircularQueue {
public:
int f;
int b;
int s;
int c;
vector<int> arr;
MyCircularQueue(int k) {
f = 0;
b = 0;
s = 0;
c = k;
vector<int> v(k);
arr = v;
}
bool enQueue(int value) {
if(s==c) return false;
arr[b] = value;
b++;
if(b==c) b = 0; //IMPORTANT
s++;
return true;
}
bool deQueue() {
if(s==0) return false;
f++;
if(f==c) f = 0; //IMPORTANT
s--;
return true;
}
int Front() {
if(s==0) return -1;
return arr[f];
}
int Rear() {
if(s==0) return -1;
if(b==0) return arr[c-1]; //IMPORTANT
return arr[b-1];
}
bool isEmpty() {
if(s==0) return true;
else return false;
}
bool isFull() {
if(s==c) return true;
else return false;
}
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue* obj = new MyCircularQueue(k);
* bool param_1 = obj->enQueue(value);
* bool param_2 = obj->deQueue();
* int param_3 = obj->Front();
* int param_4 = obj->Rear();
* bool param_5 = obj->isEmpty();
* bool param_6 = obj->isFull();
*/