Stack Problems


  1. Write a program that will create a linked list (using ListNode) of 20 Integers (random). Place the elements of the Linked List into a stack. Pop off the elements of the Stack and change values in the Linked List (use setValue()). The first element popped off the Stack should go into first place in the Linked List, the second value popped off should replace the second element in the linked list etc. The values in the Linked List should now be reversed.

  2. Create a reverse directions class that uses a stack to store directions :
    For example : If you travel
    Forward 20
    Right 10
    Left 6
    Back 12
    Left 6
    Forward 4
    End

    The computer will store the directions in a stack that is holding Direction Objects (a direction(String) and an int). End will not be stored in the stack.

    printReverse() will give you the direction back home. For the above it will print :
    Back 4
    Right 6
    Forward 12
    Right 6
    Left 10
    Back 20


    Store the command in the Stack in the order that you went. Take in directions from the keyboard (2 pieces of info for each move). Translate direction back after you pop off Direction.

    You should have member functions
    public void enter();
    public void printReverse();

  3. Fill a stack with the numbers 1 - 52 (use Integer Objects for numbers). Write a method

    public void moveToTop(Stack st, int n)
    // precondition : n <= 52

    It should take the nth Intger Object in your stack and moves it to the top leaving the rest of the stack unchanged. You may use a temporary stack to help you.
  4. Fill a stack with 5 unique numbers. Pop and store each element until Stack is empty using recursion to loop. Then, push 2 copies of the elements that have been popped back onto the stack.