//use arrays to implement stack classArrayStack { privateint maxSize; //size of stack privateint[] stack; //data privateinttop= -1; //top of the stack, initiated as -1
publicbooleanisFull() { return top == maxSize - 1; }
publicbooleanisEmpty() { return top == -1; }
//push publicvoidpush(int value) { if (isFull()) { System.out.println("The stack has been full. Can't push new value in it."); return; } top++; stack[top] = value; }
//pop publicintpop() { if (isEmpty()) { thrownewRuntimeException("The stack has been empty. Can't pop."); }
intvalue= stack[top]; top--; return value; }
//iterate (from top to bottom) publicvoidshowStack() { if (isEmpty()) { System.out.println("The stack is empty."); return; }
for (inti= top; i >= 0; i--) { System.out.println("stack[" + i + "] = " + stack[i]); } } }