Hey I am trying to create two stacks, using a while loop to input ints from the keyboard until a negative int is input onto the stack. THis is what I have so far. import java.io.*; // for I/O ////////////////////////////////////////////////////////////// class StackX { private int maxSize; // size of stack array private int[] stackArray; private int top; // top of stack //-------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; // set array size stackArray = new int[maxSize]; // create array top = -1; // no items yet
} //-------------------------------------------------------------- public void push(int j) // put item on top of stack { stackArray[++top] = j; // increment top, insert item }
//-------------------------------------------------------------- publi c int pop() // take item from top of stack { return stackArray[top--]; // access item, decrement top } //-------------------------------------------------------------- public int peek() // peek at top of stack { return stackArray[top]; } //---------------------------------------------------------- ---- public boolean isEmpty() // true if stack is empty { return (top == -1); } //-------------------------------------------------------------- public boolean isFull() // true if stack is full { return (top == maxSize-1); } //-------------------------------------------------------------- } // end class StackX
public static void main(String[] args)throws IOException {
StackX theStack = new StackX(5); // make new stack StackX bigStack = new StackX(5);
// (1) Insert input loop here
while( !theStack.isEmpty() ) { // until stack is empty, // delete top item from stack int value = theStack.pop(); System.out.print(value); // display it System.out.print(" "); } // end while