Skip to content

Commit 9ebe6ce

Browse files
committed
Added circular deque
1 parent be4ebb1 commit 9ebe6ce

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

Medium/Design Circular Deque.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
class MyCircularDeque {
2+
3+
/** Initialize your data structure here. Set the size of the deque to be k. */
4+
Queue<Integer> main;
5+
Queue<Integer> backup;
6+
int capacity;
7+
8+
public MyCircularDeque(int k) {
9+
main = new LinkedList<>();
10+
backup = new LinkedList<>();
11+
capacity = k;
12+
}
13+
14+
/** Adds an item at the front of Deque. Return true if the operation is successful. */
15+
public boolean insertFront(int value) {
16+
if (main.size() == capacity) {
17+
return false;
18+
}
19+
20+
while(!main.isEmpty()) {
21+
backup.add(main.remove());
22+
}
23+
24+
main.add(value);
25+
26+
while (!backup.isEmpty()) {
27+
main.add(backup.remove());
28+
}
29+
30+
return true;
31+
}
32+
33+
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
34+
public boolean insertLast(int value) {
35+
if (main.size() == capacity) {
36+
return false;
37+
}
38+
39+
main.add(value);
40+
41+
return true;
42+
}
43+
44+
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
45+
public boolean deleteFront() {
46+
if (main.isEmpty()) {
47+
return false;
48+
}
49+
50+
main.remove();
51+
52+
return true;
53+
}
54+
55+
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
56+
public boolean deleteLast() {
57+
if (main.isEmpty()) {
58+
return false;
59+
}
60+
61+
while(!main.isEmpty()) {
62+
backup.add(main.remove());
63+
}
64+
65+
int size = backup.size();
66+
67+
while (size-- > 1) {
68+
main.add(backup.remove());
69+
}
70+
71+
backup.remove();
72+
73+
return true;
74+
}
75+
76+
/** Get the front item from the deque. */
77+
public int getFront() {
78+
if (main.isEmpty()) {
79+
return -1;
80+
}
81+
82+
return main.peek();
83+
}
84+
85+
/** Get the last item from the deque. */
86+
public int getRear() {
87+
if (main.isEmpty()) {
88+
return -1;
89+
}
90+
91+
while(!main.isEmpty()) {
92+
backup.add(main.remove());
93+
}
94+
95+
int size = backup.size();
96+
97+
while (size-- > 1) {
98+
main.add(backup.remove());
99+
}
100+
101+
int ans = backup.peek();
102+
main.add(backup.remove());
103+
104+
return ans;
105+
}
106+
107+
/** Checks whether the circular deque is empty or not. */
108+
public boolean isEmpty() {
109+
return main.isEmpty();
110+
}
111+
112+
/** Checks whether the circular deque is full or not. */
113+
public boolean isFull() {
114+
return main.size() == capacity;
115+
}
116+
}
117+
118+
/**
119+
* Your MyCircularDeque object will be instantiated and called as such:
120+
* MyCircularDeque obj = new MyCircularDeque(k);
121+
* boolean param_1 = obj.insertFront(value);
122+
* boolean param_2 = obj.insertLast(value);
123+
* boolean param_3 = obj.deleteFront();
124+
* boolean param_4 = obj.deleteLast();
125+
* int param_5 = obj.getFront();
126+
* int param_6 = obj.getRear();
127+
* boolean param_7 = obj.isEmpty();
128+
* boolean param_8 = obj.isFull();
129+
*/

0 commit comments

Comments
 (0)