-
Notifications
You must be signed in to change notification settings - Fork 14
/
Deque.java
299 lines (279 loc) · 9.42 KB
/
Deque.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package assignment2;
import edu.princeton.cs.introcs.StdOut;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Generic data type which represents double-ended queue or deque (pronounced "deck").
* It is a generalization of a stack and a queue that supports adding and removing items
* from either the front or the back of the data structure. For more complexity current
* implementation is based on the array, but it can be based on the {@link java.util.LinkedList}
* as well.
*
* @param <Item> generic type that is stored in {@code Deque}
* @author Alex Ilyenko
* @see java.lang.Iterable
*/
public class Deque<Item> implements Iterable<Item> {
/**
* Constant holding initial array size
*/
private static final int MIN_ARRAY_SIZE = 2;
/**
* Array structure holding all elements
*/
private Item[] items;
/**
* Values needed for adding and removing operations
*/
private int itemCount, firstPosition, lastPosition;
/**
* Creates an empty {@code Deque} with array implementation.
* Default array length is 2.
*
* @see #MIN_ARRAY_SIZE
*/
@SuppressWarnings("unchecked")
public Deque() {
itemCount = 0;
items = (Item[]) new Object[MIN_ARRAY_SIZE];
firstPosition = 0;
lastPosition = 1;
}
/**
* Checks if {@code Deque} is empty
*
* @return {@code true} for empty {@code Deque} and
* {@code false} if {@code Deque} has at least one item
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns the number of items in {@code Deque}
*
* @return {@code int} representing number of items
*/
public int size() {
return itemCount;
}
/**
* Adds given item to the front of the {@code Deque} and
* increases number of items in it by one. Also this method can
* resize the array if there is no extra space for inserted item
*
* @param item given item to add
* @throws java.lang.NullPointerException if item == {@code null}
* @see #resize(int, assignment2.Deque.Side)
* @see #items
*/
public void addFirst(Item item) {
if (item == null) {
throw new NullPointerException("You can not add Null to deque!");
}
if (firstPosition < 0) {
resize(items.length + itemCount, Side.FRONT);
}
items[firstPosition--] = item;
itemCount++;
}
/**
* Adds given item to the end of the {@code Deque} and
* increases number of items in it by one. Also this method can
* resize the array if there is no extra space for inserted item
*
* @param item given item to add
* @throws java.lang.NullPointerException if item == {@code null}
* @see #resize(int, assignment2.Deque.Side)
* @see #items
*/
public void addLast(Item item) {
if (item == null) {
throw new NullPointerException("You can not add Null to deque!");
}
if (lastPosition == items.length) {
resize(items.length + itemCount, Side.END);
}
items[lastPosition++] = item;
itemCount++;
}
/**
* Removes and returns the item form the front of the {@code Deque}. This method
* decreases the number of items in the {@code Deque} by one and can shrink the array
* if there is a lot extra space in the front of it.
*
* @return item that was removed from the {@code Deque}
* @throws java.util.NoSuchElementException if {@code Deque} is already empty
* @see #resize(int, assignment2.Deque.Side)
* @see #items
*/
public Item removeFirst() {
if (isEmpty()) {
throw new NoSuchElementException("Deque is already empty!");
}
Item item = items[++firstPosition];
items[firstPosition] = null;
itemCount--;
if (itemCount > 0 && firstPosition + items.length - lastPosition >= itemCount << 2) {
resize(itemCount << 1, Side.BOTH);
}
return item;
}
/**
* Removes and returns the item form the end of the {@code Deque}. This method
* decreases the number of items in the {@code Deque} by one and can shrink the array
* if there is a lot extra space in the end of it.
*
* @return item that was removed from the {@code Deque}
* @throws java.util.NoSuchElementException if {@code Deque} is already empty
* @see #resize(int, assignment2.Deque.Side)
* @see #items
*/
public Item removeLast() {
if (isEmpty()) {
throw new NoSuchElementException("Deque is already empty!");
}
Item item = items[--lastPosition];
items[lastPosition] = null;
itemCount--;
if (itemCount > 0 && firstPosition + items.length - lastPosition >= itemCount << 2) {
resize(itemCount << 1, Side.BOTH);
}
return item;
}
/**
* Gets the {@code Iterator} for iterating through all elements in {@code Deque} in order from front to end
*
* @return {@code Iterator}
* @see java.util.Iterator
*/
public Iterator<Item> iterator() {
return new Iterator<Item>() {
/**
* Default position of {@code Iterator} is first position of item in {@code Deque}
* @see assignment2.Deque#firstPosition
*/
private int cursor = firstPosition;
/**
* Checks if {@code Deque} has next item for iterating
* @return {@code true} if {@code Deque} has left items for iterating and {@code false} if not
* @see assignment2.Deque#lastPosition
*/
@Override
public boolean hasNext() {
return cursor != lastPosition - 1;
}
/**
* Returns next item in {@code Deque} in order from front to end
* @return next item
* @throws java.util.NoSuchElementException if there is no elements left in {@code Deque}
* @see #hasNext()
*/
@Override
public Item next() {
if (!hasNext()) {
throw new NoSuchElementException("You've already reached the end of deque!");
}
return items[++cursor];
}
/**
* Unsupported remove operation
* @throws java.lang.UnsupportedOperationException
*/
@Override
public void remove() {
throw new UnsupportedOperationException("Remove action is not supported!");
}
};
}
/**
* Increases or decreases the size of the array with items by given capacity from given side.
* This method does nothing if capacity is less than the number of items in {@code Deque}
*
* @param capacity size of the new array
* @param side {@code Side} of the resizing
* @see assignment2.Deque.Side
* @see java.lang.System#arraycopy(Object, int, Object, int, int)
*/
@SuppressWarnings("unchecked")
private void resize(int capacity, Side side) {
if (capacity < itemCount) {
return;
}
Item[] tmpArr = (Item[]) new Object[capacity];
int destinationPosition = 0;
int length = lastPosition;
int start = 0;
switch (side){
case BOTH :
start = firstPosition + 1;
destinationPosition = (capacity - itemCount) >> 1;
firstPosition = destinationPosition - 1;
length = itemCount;
lastPosition = destinationPosition + length;
break;
case FRONT:
if (capacity < items.length) {
start = firstPosition;
int difference = items.length - capacity;
firstPosition -= difference;
lastPosition -= difference;
destinationPosition = firstPosition;
length = itemCount + 1;
} else {
firstPosition = itemCount - 1;
lastPosition += itemCount;
destinationPosition = length = itemCount;
}
break;
default:
// no actions required if side is END
break;
}
System.arraycopy(items, start, tmpArr, destinationPosition, length);
items = tmpArr;
}
/**
* Enum representing sides of the {@code Deque}
*/
private enum Side {
/**
* Front side
*/
FRONT,
/**
* End side
*/
END,
/**
* Both sides
*/
BOTH
}
public static void main(String[] args) {
Deque<Integer> deque = new Deque<>();
for (int i = 0; i < 12; i++) {
if (i % 2 == 0) {
deque.addFirst(i);
} else {
deque.addLast(i);
}
}
StdOut.println(deque.size());
for (int i = 0; i < 12; i++) {
if (i % 3 == 0) {
deque.removeLast();
} else {
deque.removeFirst();
}
}
StdOut.println(deque.size());
for (int i = 0; i < 12; i++) {
if (i % 2 == 0) {
deque.addFirst(i);
} else {
deque.addLast(i);
}
}
StdOut.println(deque.size());
}
}