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