Solutions to the Practice Problems#

Java Programs Using a Stack#

  1. Here is the code using my name.

    Stack<Character> name = new Stack<>();
    name.push('R');
    name.push('o');
    name.push('s');
    name.push('a');
    name.push('n');
    name.push('n');
    name.push('a');
    
    while (!name.empty()){
       System.out.print(name.pop());
    }
    System.out.println();
    
  2. The stack would have been created: Stack<Integer> banana = new Stack<>(); and items added.
    a. banana.push(banana.pop() * 2);
    b.

    Integer holdTop = banana.pop();
    Integer sum = holdTop + banana.peek();
    banana.push(holdTop);
    banana.push(sum);
    
  3. Order really matters.

    import java.util.Stack;
    
    public class JavaStacks {
    
        public static void main(String[] args) {
            Stack<Integer> myData = new Stack<>();
            Stack<Integer> helperStack = new Stack<>();
         
            myData.push(12);
            myData.push(15);
            myData.push(19);
         
            helperStack.push(myData.pop()); //19
            helperStack.push(myData.pop()); //15
            helperStack.push(myData.pop()); //12
            myData.push(4);
            myData.push(helperStack.pop()); //12
            myData.push(helperStack.pop()); //15
            myData.push(helperStack.pop()); //19
         
            helperStack.push(myData.pop()); //19
            helperStack.push(myData.pop()); //15
            helperStack.push(myData.pop()); //12
            helperStack.push(myData.pop()); //4
            myData.push(3);
            myData.push(helperStack.pop()); //4
            myData.push(helperStack.pop()); //12
            myData.push(helperStack.pop()); //15
            myData.push(helperStack.pop()); //19
         
            myData.push(35);
         
            helperStack.push(myData.pop()); //35
            myData.push(20);
            myData.push(helperStack.pop()); //35
         
            myData.push(36);
         
            helperStack.push(myData.pop()); //36
            helperStack.push(myData.pop()); //35
            helperStack.push(myData.pop()); //20
            helperStack.push(myData.pop()); //19
            helperStack.push(myData.pop()); //15
            myData.push(14);
            myData.push(helperStack.pop()); //15
            myData.push(helperStack.pop()); //19
            myData.push(helperStack.pop()); //20
            myData.push(helperStack.pop()); //35
            myData.push(helperStack.pop()); //36
         
            myData.push(42);
            System.out.println("The final stack is " + myData);
        }//main
    }//class
    
  1. Better code, from a readability standpoint.

import java.util.Stack;

public class JavaStacks {

    public static void main(String[] args) {
        Stack<Integer> myData = new Stack<>();
     
        addSorted(12, myData);
        addSorted(15, myData);
        addSorted(19, myData);
        addSorted(4, myData);
        addSorted(3, myData);
        addSorted(35, myData);
        addSorted(20, myData);
        addSorted(36, myData);
        addSorted(14, myData);
        addSorted(42, myData);
     
        System.out.println("The final stack is " + myData);
    }//main
 
    /**
     * Moves integers from the first stack onto the second stack (in
     * reverse order) until the top of the first stack is less than the
     * give number.
     * @param num - the given integer
     * @param fromStack - moving off the top of this stack
     * @param toStack - moving onto the top of this stack
     */
    public static void moveUntilLess(Integer num, Stack<Integer> fromStack, 
                                     Stack<Integer> toStack){
        while (!fromStack.empty() && fromStack.peek() > num){
            toStack.push(fromStack.pop());
        }
    }//moveUntilLess
 
    /**
     * Moves everything on the first stack to the top of the second stack,
     * in reverse order.
     * @param fromStack - moves out of here
     * @param toStack - moves into here, in reverse order
     */
    public static void moveAll(Stack<Integer> fromStack, 
                               Stack<Integer> toStack){
        while (!fromStack.empty()){
            toStack.push(fromStack.pop());
        }
    }//moveAll
 
    /**
     * Adds an item to an Integer stack to maintain a sorted order.  NOTE: 
     * the stack is assumed sorted to start with.  This is not a sort method.
     * This is a method to maintain a sorted stack, by inserting the number
     * in the proper place.  The sort is ascending, from bottom to top.
     * @param num - the Integer to insert
     * @param theStack - the sorted stack of Integers, ascending from
     *       from bottom to top
     */
    public static void addSorted(Integer num, Stack<Integer> theStack){
        Stack<Integer> helperStack = new Stack<>();
        moveUntilLess(num, theStack, helperStack);
        theStack.push(num);
        moveAll(helperStack, theStack);       
    }//addSorted
}//class