|
| 1 | +package popular; |
| 2 | + |
| 3 | + |
| 4 | +class DoubleLinkNode { |
| 5 | + public DoubleLinkNode pre; |
| 6 | + public DoubleLinkNode next; |
| 7 | + public int val; |
| 8 | + public DoubleLinkNode(int val) { |
| 9 | + this.val = val; |
| 10 | + } |
| 11 | + |
| 12 | +} |
| 13 | + |
| 14 | +public class MyCircularDeque { |
| 15 | + int size; |
| 16 | + int capacity; |
| 17 | + DoubleLinkNode head; |
| 18 | + DoubleLinkNode tail; |
| 19 | + |
| 20 | + /** Initialize your data structure here. Set the size the deque to be k. */ |
| 21 | + public MyCircularDeque(int k) { |
| 22 | + head = new DoubleLinkNode(-1); |
| 23 | + tail = new DoubleLinkNode(-1); |
| 24 | + head.next = tail; |
| 25 | + tail.pre = head; |
| 26 | + |
| 27 | + //head.next = tail; |
| 28 | + //tail.pre = head; |
| 29 | + |
| 30 | + size = 0; |
| 31 | + capacity = k; |
| 32 | + } |
| 33 | + |
| 34 | + /** Adds an item at the front of Deque. Return true if the operation is successful. */ |
| 35 | + public boolean insertFront(int value) { |
| 36 | + if (isFull()) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + DoubleLinkNode node = new DoubleLinkNode(value); |
| 41 | + node.next = head.next; |
| 42 | + node.pre = head; |
| 43 | + head.next.pre = node; |
| 44 | + head.next = node; |
| 45 | + |
| 46 | + size++; |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + /** Adds an item at the rear of Deque. Return true if the operation is successful. */ |
| 52 | + public boolean insertLast(int value) { |
| 53 | + if (isFull()) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + DoubleLinkNode node = new DoubleLinkNode(value); |
| 58 | + node.pre = tail.pre; |
| 59 | + node.next = tail; |
| 60 | + tail.pre.next = node; |
| 61 | + tail.pre = node; |
| 62 | + |
| 63 | + size++; |
| 64 | + |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + /** Deletes an item from the front of Deque. Return true if the operation is successful. */ |
| 69 | + public boolean deleteFront() { |
| 70 | + if (isEmpty()) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + head.next.next.pre = head; |
| 75 | + head.next = head.next.next; |
| 76 | + |
| 77 | + size--; |
| 78 | + |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + /** Deletes an item from the rear of Deque. Return true if the operation is successful. */ |
| 83 | + public boolean deleteLast() { |
| 84 | + if (isEmpty()) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + tail.pre.pre.next = tail; |
| 89 | + tail.pre = tail.pre.pre; |
| 90 | + |
| 91 | + size--; |
| 92 | + |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + /** Get the front item from the deque. */ |
| 97 | + public int getFront() { |
| 98 | + return head.next.val; |
| 99 | + } |
| 100 | + |
| 101 | + /** Get the last item from the deque */ |
| 102 | + public int getRear() { |
| 103 | + |
| 104 | + return tail.pre.val; |
| 105 | + } |
| 106 | + |
| 107 | + /** Checks whether the circular deque is empty or not. */ |
| 108 | + public boolean isEmpty() { |
| 109 | + return size == 0; |
| 110 | + } |
| 111 | + |
| 112 | + /** Checks whether the circular deque is full or not. */ |
| 113 | + public boolean isFull() { |
| 114 | + return size == capacity; |
| 115 | + } |
| 116 | + |
| 117 | + public static void main(String[] args) { |
| 118 | + /** Your MyCircularDeque object will be instantiated and called as such: */ |
| 119 | + //["MyCircularDeque","insertFront","deleteLast","getRear","getFront","getFront","deleteFront","insertFront","insertLast","insertFront","getFront","insertFront"] |
| 120 | + //[[4],[9],[],[],[],[],[],[6],[5],[9],[],[6]] |
| 121 | + int k = 1000; |
| 122 | + MyCircularDeque obj = new MyCircularDeque(k); |
| 123 | + boolean param_1 = obj.insertFront(1); |
| 124 | + boolean param_2 = obj.insertLast(20); |
| 125 | + int param_5 = obj.getFront(); |
| 126 | + int param_6 = obj.getRear(); |
| 127 | + boolean param_3 = obj.deleteFront(); |
| 128 | + boolean param_4 = obj.deleteLast(); |
| 129 | + boolean param_7 = obj.isEmpty(); |
| 130 | + boolean param_8 = obj.isFull(); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
0 commit comments