// stack.java // demonstrates stacks // to run this program: C>java StackApp //////////////////////////////////////////////////////////////// class StackX { privateint maxSize; // size of stack array privatelong[] stackArray; privateint top; // top of stack //-------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; // set array size stackArray =newlong[maxSize]; // create array top =-1; // no items yet } //-------------------------------------------------------------- publicvoid push(long j) // put item on top of stack { stackArray[++top] = j; // increment top, insert item } //-------------------------------------------------------------- publiclong pop() // take item from top of stack { return stackArray[top--]; // access item, decrement top } //-------------------------------------------------------------- publiclong peek() // peek at top of stack { return stackArray[top]; } //-------------------------------------------------------------- publicboolean isEmpty() // true if stack is empty { return (top ==-1); } //-------------------------------------------------------------- publicboolean isFull() // true if stack is full { return (top == maxSize-1); } //-------------------------------------------------------------- }// end class StackX //////////////////////////////////////////////////////////////// class StackApp { publicstaticvoid main(String[] args) { StackX theStack =new StackX(10); // make new stack theStack.push(20); // push items onto stack theStack.push(40); theStack.push(60); theStack.push(80); while( !theStack.isEmpty() ) // until it's empty, { // delete item from stack long value = theStack.pop(); System.out.print(value); // display it System.out.print(""); }// end while System.out.println(""); }// end main() }// end class StackApp ////////////////////////////////////////////////////////////////