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
|
public class MyStack{ private int[] elem; private int top; private int bottom; private int usedSize;
public MyStack(){ this.elem = new int[10]; }
public void print(){ for (int i = 0; i < usedSize; i++) { System.out.print(elem[i]+" "); } System.out.println(); }
public void addSize(){ this.elem = Arrays.copyOf(this.elem, this.elem.length * 2); }
public boolean isFull(){ return this.elem.length==usedSize; }
public void push(int val){ if(isFull()){ addSize(); } this.elem[usedSize] = val; this.usedSize++; this.top++; }
public int pop(){ if(this.top < this.bottom) return -1; this.top = this.top - 1; this.usedSize = this.usedSize - 1; return this.elem[this.usedSize]; }
public int peek(){ return this.elem[this.usedSize - 1]; }
}
|