File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments