Skip to content

Commit afa0694

Browse files
authored
Create 641. Design Circular Deque (#598)
2 parents fc7e442 + f09b2a7 commit afa0694

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

641. Design Circular Deque

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class MyCircularDeque {
2+
public:
3+
const int k;
4+
vector<int> q;
5+
int size = 0;
6+
int front = 0;
7+
int rear;
8+
MyCircularDeque(int k): k(k), q(k), rear(k - 1){
9+
10+
}
11+
12+
bool insertFront(int value) {
13+
if(isFull()){
14+
return false;
15+
}
16+
front = (--front + k) % k;
17+
q[front] = value;
18+
size++;
19+
return true;
20+
}
21+
22+
bool insertLast(int value) {
23+
if(isFull()){
24+
return false;
25+
}
26+
rear = ++rear % k;
27+
q[rear] = value;
28+
size++;
29+
return true;
30+
}
31+
32+
bool deleteFront() {
33+
if(isEmpty()){
34+
return false;
35+
}
36+
front = ++front % k;
37+
size--;
38+
return true;
39+
}
40+
41+
bool deleteLast() {
42+
if(isEmpty()){
43+
return false;
44+
}
45+
rear = (--rear + k)%k;
46+
size--;
47+
return true;
48+
}
49+
50+
int getFront() {
51+
return isEmpty() ? -1 : q[front];
52+
}
53+
54+
int getRear() {
55+
return isEmpty() ? -1 : q[rear];
56+
}
57+
58+
bool isEmpty() {
59+
return size == 0;
60+
}
61+
62+
bool isFull() {
63+
return size == k;
64+
}
65+
};

0 commit comments

Comments
 (0)