-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathArrayDeque.java
159 lines (144 loc) · 4.16 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
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
/**
* Deque implemented by array.
*/
public class ArrayDeque<T> implements Deque<T> {
private T[] items;
private int left;
private int right;
private int capacity = 8;
public ArrayDeque() {
items = (T[]) new Object[capacity];
left = right = 0;
}
/** Adds an item of type T to the front of the deque. */
@Override
public void addFirst(T item) {
if (isFull()) {
resize((int) (capacity * 1.5));
}
left = (left - 1 + capacity) % capacity;
items[left] = item;
}
/** Adds an item of type T to the back of the deque. */
@Override
public void addLast(T item) {
if (isFull()) {
resize((int) (capacity * 1.5));
}
items[right] = item;
right = (right + 1 + capacity) % capacity;
}
/** Returns true if deque is empty, false otherwise. */
@Override
public boolean isEmpty() {
return left == right;
}
/** Returns the number of items in the deque. */
@Override
public int size() {
return (right - left + capacity) % capacity;
}
/** Prints the items in the deque from first to last, separated by a space. */
@Override
public void printDeque() {
if (left < right) {
for (int i = left; i < right; i++) {
if (i == right - 1) {
System.out.println(items[i]);
break;
}
System.out.print(items[i] + " ");
}
} else if (left > right) {
for (int i = left; i < capacity; i++) {
System.out.print(items[i] + " ");
}
for (int i = 0; i < right; i++) {
if (i == right - 1) {
System.out.println(items[i]);
break;
}
System.out.print(items[i] + " ");
}
}
}
/**
* Removes and returns the item at the front of the deque. If no such item
* exists, returns null.
*/
@Override
public T removeFirst() {
if (isEmpty()) {
return null;
}
T res = items[left];
left = (left + 1) % capacity;
if (isLowUsageRate()) {
resize((int) (capacity * 0.5));
}
return res;
}
/**
* Removes and returns the item at the back of the deque. If no such item
* exists, returns null.
*/
@Override
public T removeLast() {
if (isEmpty()) {
return null;
}
right = (right - 1 + capacity) % capacity;
T res = items[right];
if (isLowUsageRate()) {
resize((int) (capacity * 0.5));
}
return res;
}
/**
* Gets the item at the given index, where 0 is the front, 1 is the next item,
* and so forth. If no such item exists, returns null. Must not alter the deque!
*/
@Override
public T get(int index) {
if (index < 0 || index >= size() || isEmpty()) {
return null;
}
if (left < right) {
return items[index + left];
} else if (left > right) {
if (index + left < capacity) {
return items[index + left];
} else {
return items[(index + left) % capacity];
}
}
return null;
}
private boolean isFull() {
return size() == capacity - 1;
}
private boolean isLowUsageRate() {
return capacity >= 16 && size() / (double) capacity < 0.25;
}
private void resize(int newSize) {
T[] newArray = (T[]) new Object[newSize];
int size = size();
if (left < right) {
for (int i = left, j = 0; i < right && j < size; i++, j++) {
newArray[j] = items[i];
}
} else if (left > right) {
int j = 0;
for (int i = left; j < capacity - left; i++, j++) {
newArray[j] = items[i];
}
for (int i = 0; j < size; i++, j++) {
newArray[j] = items[i];
}
}
left = 0;
right = size;
items = newArray;
capacity = newSize;
}
}