-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayDeque.java
130 lines (112 loc) · 3.59 KB
/
ArrayDeque.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
public class ArrayDeque<T> implements Deque<T> {
private int size;
private T[] items;
private int nextFirst;
private int nextBack;
private int increment(int index) {
return (index + 1) % items.length;
}
private int decrement(int index) {
return (index - 1 + items.length) % items.length;
}
private void resize(int len) {
T[] temp = (T[]) new Object[len];
if (len < items.length) {
System.arraycopy(items, increment(nextFirst), temp, 0, size);
} else {
System.arraycopy(items, increment(nextFirst), temp, 0,
items.length - increment(nextFirst));
System.arraycopy(items, 0, temp, items.length - increment(nextFirst), nextFirst);
}
items = temp;
nextFirst = items.length - 1;
nextBack = size;
}
@Override//Adds an item T to the front of the deque
public void addFirst(T item) {
if (nextFirst == nextBack && size == items.length - 1) {
resize(items.length * 2);
}
items[nextFirst] = item;
nextFirst = decrement(nextFirst);
size++;
}
@Override//Adds an item T to the back to the deque
public void addLast(T item) {
if (nextFirst == nextBack && size == items.length - 1) {
resize(items.length * 2);
}
items[nextBack] = item;
nextBack = increment(nextBack);
size++;
}
/*Returns whether or not the deque is empty
public boolean isEmpty() {
return size == 0;
}*/
@Override//Returns the number of items in the deque
public int size() {
return size;
}
@Override//Prints the elements in the deque from first to last separated by a space
public void printDeque() {
for (int i = 0; i < size; i++) {
System.out.print(this.get(i) + " ");
}
}
@Override//Removes and returns the first item in the deque
public T removeFirst() {
if (size == 0) {
return null;
}
T answer = items[increment(nextFirst)];
items[increment(nextFirst)] = null;
nextFirst = increment(nextFirst);
size -= 1;
if (size != 0 && items.length >= 16 && (double) size / items.length < 0.25) {
resize(items.length / 2);
}
return answer;
}
@Override//Removes and returns the last item in the deque
public T removeLast() {
if (size == 0) {
return null;
}
T answer;
answer = items[decrement(nextBack)];
items[decrement(nextBack)] = null;
nextBack = decrement(nextBack);
size -= 1;
if (size != 0 && items.length >= 16 && (double) size / items.length < 0.25) {
resize(items.length / 2);
}
return answer;
}
@Override//Returns the item at the given index
public T get(int index) {
if (index >= 0 && index < items.length) {
return items[increment(nextFirst + index)];
}
return null;
}
//Creates an empty array deque
public ArrayDeque() {
items = (T[]) new Object[8];
size = 0;
nextFirst = 0;
nextBack = 1;
}
//Creates a deep copy of other
public ArrayDeque(ArrayDeque other) {
items = (T[]) new Object[other.items.length];
int counter = 0;
while (counter < other.items.length) {
items[counter] = (T) other.items[counter];
counter += 1;
}
this.size = other.size;
this.nextFirst = other.nextFirst;
this.nextBack = other.nextBack;
}
}