-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path641. Design Circular Deque
65 lines (56 loc) · 1.14 KB
/
641. Design Circular Deque
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 MyCircularDeque {
public:
const int k;
vector<int> q;
int size = 0;
int front = 0;
int rear;
MyCircularDeque(int k): k(k), q(k), rear(k - 1){
}
bool insertFront(int value) {
if(isFull()){
return false;
}
front = (--front + k) % k;
q[front] = value;
size++;
return true;
}
bool insertLast(int value) {
if(isFull()){
return false;
}
rear = ++rear % k;
q[rear] = value;
size++;
return true;
}
bool deleteFront() {
if(isEmpty()){
return false;
}
front = ++front % k;
size--;
return true;
}
bool deleteLast() {
if(isEmpty()){
return false;
}
rear = (--rear + k)%k;
size--;
return true;
}
int getFront() {
return isEmpty() ? -1 : q[front];
}
int getRear() {
return isEmpty() ? -1 : q[rear];
}
bool isEmpty() {
return size == 0;
}
bool isFull() {
return size == k;
}
};