Say if i have created two different stacks,and any integer over 10 goes in one stack(bigStack). If less than 10 goes in one stack(theStack). The program is already working where it will resize if its full,but I am trying to put the integers in the right Stack. This is what I have so far.
import java.io.*; ////////////////////////////////////////////////////////////// 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 if(top == stackArray.length-1) // stack is full { maxSize = maxSize*2; int [] newArray = new int[maxSize]; for(int i =0; i<= top; i++)newArray = stackArray;
stackArray = newArray;
System.out.println("Big Stack Increased To "+maxSize); }
}
//---------------------------------------------------------- ---- public 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);
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