public class MyArrayStack<Item> implements Iterator<Item> {
private Item[] a = (Item[]) new Object[1];
private int N = 0;
public boolean isEmpty() {return N == 0;}
public int size() {return N;}
private void resize(int cap) {
Item[] tmp = (Item[]) new Object(cap);
for(int i = 0; i < a.length; i++) {
tmp[i] = a[i];
}
a = tmp;
}
public void push(Item item) {
if(a.length == N) resize(a.length * 2);
//what is missing here?
}
public Item pop() {
Item item = a[--N];
a[N] = null;
if(N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
public Iterator<Item> iterate() {
return new MyReverseIterator();
}
private class MyReverseIterator implements Iterator<Item> {
int i = N;
public boolean hasNext() {
return i > 0;
}
public Item next() {
return a[--i];
}
}
}