| 1 | package Stack; |
| 2 | |
| 3 | public class ArrayStack<E> implements IStack<E> { |
| 4 | private static final int CAPACITY = 1000; |
| 5 | private E[] data; |
| 6 | private int top = -1; |
| 7 | |
| 8 | public ArrayStack() { |
| 9 | this(CAPACITY); |
| 10 | } |
| 11 | |
| 12 | public ArrayStack(int capacity) { |
| 13 | data = (E[]) new Object[capacity]; |
| 14 | } |
| 15 | |
| 16 | @Override |
| 17 | public int size() { |
| 18 | return top + 1; |
| 19 | } |
| 20 | |
| 21 | @Override |
| 22 | public boolean isEmpty() { |
| 23 | return top == -1; |
| 24 | } |
| 25 | |
| 26 | @Override |
| 27 | public void push(E e) throws IllegalStateException { |
| 28 | if (size() == data.length) throw new IllegalStateException("Stack is full"); |
| 29 | data[++top] = e; |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public E pop() { |
| 34 | if (isEmpty()) return null; |
| 35 | E target = data[top]; |
| 36 | data[top] = null; // dereference to help garbage collection |
| 37 | top--; |
| 38 | return target; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public E peek() { |
| 43 | if (isEmpty()) return null; |
| 44 | return data[top]; |
| 45 | } |
| 46 | } |
nothing calls this directly
no outgoing calls
no test coverage detected