-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResizingArrayQueue.java
131 lines (95 loc) · 3.01 KB
/
ResizingArrayQueue.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
package DataStructure;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<Key> implements Iterable<Key> {
private Key[] items;
private int size;
private int first, last;
public ResizingArrayQueue(int capacity) {
items = (Key[]) new Object[capacity];
size = 0;
first = capacity - 1;
last = capacity - 1;
}
public ResizingArrayQueue() {
this(1);
}
public void enqueue(Key key) {
if (size == items.length) resize(items.length * 2);
items[last++] = key;
size++;
if (last == items.length) last = 0;
}
public Key dequeue() {
if (isEmpty()) throw new NoSuchElementException("Empty resizing array stack!");
Key key = items[first];
items[first++] = null;
size--;
if (first == items.length) first = 0;
if (size > 0 && size == items.length / 4) resize(items.length / 2);
return key;
}
public Key peek() {
return items[first];
}
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
private void resize(int capacity) {
Key[] newItems = (Key[]) new Object[capacity];
for (int i = 0; i < size; i++) {
newItems[i] = items[(first + i) % size];
}
first = 0;
last = size;
items = newItems;
}
public Iterator<Key> iterator() {
return new ArrayIterator();
}
private class ArrayIterator implements Iterator<Key> {
private int n, current;
public ArrayIterator() {
n = size;
current = first;
}
public boolean hasNext() {
return n > 0;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Key next() {
if (!hasNext()) throw new NoSuchElementException("Empty resizing array stack.");
Key key = items[current++];
if (current == items.length) current = 0;
size--;
return key;
}
}
public static void main(String[] args) {
ResizingArrayQueue arrayQueue = new ResizingArrayQueue();
for (int i = 0; i < 1000; i++) {
arrayQueue.enqueue(i);
}
if (arrayQueue.size() != 1000) throw new Error("Wrong size");
for (int i = 0; i < 1000; i++) {
if (arrayQueue.dequeue() != (Integer) i) throw new Error();
}
try {
arrayQueue.dequeue();
} catch (NoSuchElementException a) {
System.out.println("Worked!");
}
arrayQueue.enqueue(1);
arrayQueue.enqueue(2);
if (arrayQueue.peek() != arrayQueue.dequeue()) throw new Error("Peek or pop doesn't work!");
arrayQueue.enqueue(1);
arrayQueue.enqueue(2);
if (arrayQueue.peek() != (Integer) 1) throw new Error("Peek doesn't work!");
System.out.println("Tests pass!");
}
}