|
1 | 1 | package dataStructures.queue;
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * This class implements a doubly-ended queue, known as a deque. |
| 5 | + * |
| 6 | + * @param <T> The type of the element to be stored witin the deque. |
| 7 | + */ |
3 | 8 | public class Deque<T> {
|
4 | 9 |
|
5 |
| - private Node<T> first; |
6 |
| - private Node<T> last; |
7 |
| - private int size; |
| 10 | + private Node<T> first; |
8 | 11 |
|
9 |
| - /** |
10 |
| - * Constructor for empty Deque. |
11 |
| - */ |
12 |
| - public Deque() { |
13 |
| - this.size = 0; |
14 |
| - this.first = this.last = null; // implemented with double-linked list |
15 |
| - } |
| 12 | + private Node<T> last; |
16 | 13 |
|
17 |
| - /** |
18 |
| - * Check if deque is empty. |
19 |
| - * |
20 |
| - * @return True if empty. |
21 |
| - */ |
22 |
| - public boolean isEmpty() { |
23 |
| - return this.size == 0; |
24 |
| - } |
| 14 | + private int size; |
25 | 15 |
|
26 |
| - /** |
27 |
| - * Get size of deque. |
28 |
| - * |
29 |
| - * @return Size of the deque. |
30 |
| - */ |
31 |
| - public int getSize() { |
32 |
| - return this.size; |
33 |
| - } |
| 16 | + /** |
| 17 | + * Constructor for empty Deque. |
| 18 | + */ |
| 19 | + public Deque() { |
| 20 | + this.size = 0; |
| 21 | + this.first = this.last = null; // implemented with double-linked list |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Check if deque is empty. |
| 26 | + * |
| 27 | + * @return True if empty. |
| 28 | + */ |
| 29 | + public boolean isEmpty() { |
| 30 | + return this.size == 0; |
| 31 | + } |
34 | 32 |
|
35 |
| - /** |
36 |
| - * Add element into the back of the deque. |
37 |
| - * |
38 |
| - * @param val Element to be added. |
39 |
| - */ |
40 |
| - public void addElement(T val) { |
41 |
| - Node<T> newNode = new Node<>(val); |
42 |
| - if (this.isEmpty()) { |
43 |
| - this.last = this.first = newNode; |
44 |
| - size++; |
45 |
| - return; |
46 |
| - } |
47 |
| - Node<T> temp = this.last; |
48 |
| - temp.next = newNode; |
49 |
| - newNode.prev = temp; |
50 |
| - this.last = newNode; |
51 |
| - this.size++; |
| 33 | + /** |
| 34 | + * Get size of deque. |
| 35 | + * |
| 36 | + * @return Size of the deque. |
| 37 | + */ |
| 38 | + public int getSize() { |
| 39 | + return this.size; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Add element into the back of the deque. |
| 44 | + * |
| 45 | + * @param val Element to be added. |
| 46 | + */ |
| 47 | + public void addElement(T val) { |
| 48 | + Node<T> newNode = new Node<>(val); |
| 49 | + if (this.isEmpty()) { |
| 50 | + this.last = this.first = newNode; |
| 51 | + size++; |
| 52 | + return; |
52 | 53 | }
|
| 54 | + Node<T> temp = this.last; |
| 55 | + temp.next = newNode; |
| 56 | + newNode.prev = temp; |
| 57 | + this.last = newNode; |
| 58 | + this.size++; |
| 59 | + } |
53 | 60 |
|
54 |
| - /** |
55 |
| - * Add a element into the front of the deque. |
56 |
| - * @param val Element to be added. |
57 |
| - */ |
58 |
| - public void addFirst(T val) { |
59 |
| - Node<T> newNode = new Node<>(val); |
60 |
| - if (this.isEmpty()) { |
61 |
| - this.last = this.first = newNode; |
62 |
| - size++; |
63 |
| - return; |
64 |
| - } |
65 |
| - Node<T> temp = this.first; |
66 |
| - temp.prev = newNode; |
67 |
| - newNode.next = temp; |
68 |
| - this.first = newNode; |
69 |
| - size++; |
| 61 | + /** |
| 62 | + * Add a element into the front of the deque. |
| 63 | + * |
| 64 | + * @param val Element to be added. |
| 65 | + */ |
| 66 | + public void addFirst(T val) { |
| 67 | + Node<T> newNode = new Node<>(val); |
| 68 | + if (this.isEmpty()) { |
| 69 | + this.last = this.first = newNode; |
| 70 | + size++; |
| 71 | + return; |
70 | 72 | }
|
| 73 | + Node<T> temp = this.first; |
| 74 | + temp.prev = newNode; |
| 75 | + newNode.next = temp; |
| 76 | + this.first = newNode; |
| 77 | + size++; |
| 78 | + } |
71 | 79 |
|
72 |
| - /** |
73 |
| - * Peek the first element of the deque. |
74 |
| - * |
75 |
| - * @return The value in the first node of the deque. |
76 |
| - */ |
77 |
| - public T peekFirst() { |
78 |
| - if (this.isEmpty()) { |
79 |
| - return null; |
80 |
| - } |
81 |
| - return first.val; |
| 80 | + /** |
| 81 | + * Peek the first element of the deque. |
| 82 | + * |
| 83 | + * @return The value in the first node of the deque. |
| 84 | + */ |
| 85 | + public T peekFirst() { |
| 86 | + if (this.isEmpty()) { |
| 87 | + return null; |
82 | 88 | }
|
| 89 | + return first.val; |
| 90 | + } |
83 | 91 |
|
84 |
| - /** |
85 |
| - * Peek the last element of the deque. |
86 |
| - * |
87 |
| - * @return The value in the last node of the deque. |
88 |
| - */ |
89 |
| - public T peekLast() { |
90 |
| - if (this.isEmpty()) { |
91 |
| - return null; |
92 |
| - } |
93 |
| - return last.val; |
| 92 | + /** |
| 93 | + * Peek the last element of the deque. |
| 94 | + * |
| 95 | + * @return The value in the last node of the deque. |
| 96 | + */ |
| 97 | + public T peekLast() { |
| 98 | + if (this.isEmpty()) { |
| 99 | + return null; |
94 | 100 | }
|
| 101 | + return last.val; |
| 102 | + } |
95 | 103 |
|
96 |
| - /** |
97 |
| - * Removes and retrieves the first element of the deque. |
98 |
| - * |
99 |
| - * @return The value in the first node of the deque. |
100 |
| - */ |
101 |
| - public T pollFirst() { |
102 |
| - if (this.isEmpty()) { |
103 |
| - return null; |
104 |
| - } |
105 |
| - Node<T> firstNode = this.first; |
106 |
| - Node<T> newFirstNode = this.first.next; |
107 |
| - if (newFirstNode != null) { |
108 |
| - newFirstNode.prev = null; |
109 |
| - } |
110 |
| - this.first = newFirstNode; |
111 |
| - firstNode.next = null; |
112 |
| - this.size--; |
113 |
| - return firstNode.val; |
| 104 | + /** |
| 105 | + * Removes and retrieves the first element of the deque. |
| 106 | + * |
| 107 | + * @return The value in the first node of the deque. |
| 108 | + */ |
| 109 | + public T pollFirst() { |
| 110 | + if (this.isEmpty()) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + Node<T> firstNode = this.first; |
| 114 | + Node<T> newFirstNode = this.first.next; |
| 115 | + if (newFirstNode != null) { |
| 116 | + newFirstNode.prev = null; |
114 | 117 | }
|
| 118 | + this.first = newFirstNode; |
| 119 | + firstNode.next = null; |
| 120 | + this.size--; |
| 121 | + return firstNode.val; |
| 122 | + } |
115 | 123 |
|
116 |
| - /** |
117 |
| - * Removes and retrieves the last element of the deque. |
118 |
| - * |
119 |
| - * @return The value in the last node of the deque. |
120 |
| - */ |
121 |
| - public T pollLast() { |
122 |
| - if (this.isEmpty()) { |
123 |
| - return null; |
124 |
| - } |
125 |
| - Node<T> lastNode = this.last; |
126 |
| - Node<T> newLastNode = lastNode.prev; |
127 |
| - if (newLastNode != null) { |
128 |
| - newLastNode.next = null; |
129 |
| - } |
130 |
| - lastNode.prev = null; |
131 |
| - this.last = newLastNode; |
132 |
| - this.size--; |
133 |
| - return lastNode.val; |
| 124 | + /** |
| 125 | + * Removes and retrieves the last element of the deque. |
| 126 | + * |
| 127 | + * @return The value in the last node of the deque. |
| 128 | + */ |
| 129 | + public T pollLast() { |
| 130 | + if (this.isEmpty()) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + Node<T> lastNode = this.last; |
| 134 | + Node<T> newLastNode = lastNode.prev; |
| 135 | + if (newLastNode != null) { |
| 136 | + newLastNode.next = null; |
134 | 137 | }
|
| 138 | + lastNode.prev = null; |
| 139 | + this.last = newLastNode; |
| 140 | + this.size--; |
| 141 | + return lastNode.val; |
| 142 | + } |
135 | 143 |
|
136 |
| - /** |
137 |
| - * Converts Deque into String iteratively. |
138 |
| - * |
139 |
| - * @return String representation of elements in the deque. |
140 |
| - */ |
141 |
| - @Override |
142 |
| - public String toString() { |
143 |
| - if (this.isEmpty()) { |
144 |
| - return "[]"; |
145 |
| - } |
146 |
| - String ret = "["; |
147 |
| - Node<T> temp = this.first; |
148 |
| - while (temp != null) { |
149 |
| - ret += " " + temp.toString() + " "; |
150 |
| - temp = temp.next; |
151 |
| - } |
152 |
| - return ret + "]"; |
| 144 | + /** |
| 145 | + * Converts Deque into String iteratively. |
| 146 | + * |
| 147 | + * @return String representation of elements in the deque. |
| 148 | + */ |
| 149 | + @Override |
| 150 | + public String toString() { |
| 151 | + if (this.isEmpty()) { |
| 152 | + return "[]"; |
| 153 | + } |
| 154 | + String ret = "["; |
| 155 | + Node<T> temp = this.first; |
| 156 | + while (temp != null) { |
| 157 | + ret += " " + temp.toString() + " "; |
| 158 | + temp = temp.next; |
153 | 159 | }
|
| 160 | + return ret + "]"; |
| 161 | + } |
154 | 162 |
|
155 |
| - private static class Node<T> { |
156 |
| - private T val; |
157 |
| - Node<T> next; |
158 |
| - Node<T> prev; |
| 163 | + private static class Node<T> { |
| 164 | + private T val; |
| 165 | + Node<T> next; |
| 166 | + Node<T> prev; |
159 | 167 |
|
160 |
| - public Node(T val) { |
161 |
| - this.val = val; |
162 |
| - this.next = this.prev = null; |
163 |
| - } |
| 168 | + public Node(T val) { |
| 169 | + this.val = val; |
| 170 | + this.next = this.prev = null; |
| 171 | + } |
164 | 172 |
|
165 |
| - @Override |
166 |
| - public String toString() { |
167 |
| - return this.val.toString(); |
168 |
| - } |
| 173 | + @Override |
| 174 | + public String toString() { |
| 175 | + return this.val.toString(); |
169 | 176 | }
|
| 177 | + } |
170 | 178 | }
|
0 commit comments