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
|
public class Stack<E> { private Object[] data = null ; private int maxStack = 0; private int top = -1;
Stack(){ this(10); } Stack (int initialSize){ if (initialSize >= 0) { this.maxStack = initialSize; data = new Object[initialSize]; top = -1; }else { throw new RuntimeException("can't create stack'"+ initialSize); }
} public boolean push(E e){ if (top == maxStack -1){ throw new RuntimeException("can't push stack'"); }else { data[++top] = e; return true; }
} public E pop() { if (top == -1 ) { throw new RuntimeException("stack is empty"); }else { return (E)data[top--]; } }
public E peek() { if (top == -1){ throw new RuntimeException("stack is empty"); }else{ return (E)data[top]; } }
}
|