-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizingArrayStack.java
More file actions
59 lines (51 loc) · 1.48 KB
/
Copy pathResizingArrayStack.java
File metadata and controls
59 lines (51 loc) · 1.48 KB
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
/*
* Pushdown (LIFO) stack (resziing array implementation)
*
* iterable
* ResizingArray<String> collection = new ResizingArrayStack<String>();
* ...
* for (String s : collection)
* StdOut.println(s);
* // or use a while construct
* Iterator<String> i = collection.iterator();
* while (i.hasNext()) {
* String s = i.next();
* StdOut.println(s);
* }
*/
import java.util.Iterator;
public class ResizingArrayStack<Item> implements Iterable<Item> {
private Item[] a = (Item[]) new Object[1]; // stack items
private int N = 0;
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
private void resize(int n) {
// move stack to a new array of size max.
Item[] temp = (Item[]) new Object[max];
int max = N > n ? n : N;
for (int i = 0; i < max; i++)
temp[i] = a[i];
a = temp;
}
public void push(Item item) {
// add item to top of stack
if (N == a.length) resize(2*a.length);
a[N++] = item;
}
public Item pop() {
// Remove item from top of stack
Item item = a[--N];
a[N] = null;
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
public Iterator<Item> iterator()
{ return new ReverseArrayIterator(); }
private class ReverseArrayIterator implements Iterator<Item> {
// support LIFO iterator
private int i = N;
public boolean hasNext() { return i > 0; }
public boolean next() { return a[--i]; }
public void remove() {}
}
}