Chapter 7, Exercise 1

 

 package hw6;

 

import java.util.Stack;

 

public class Hw6 {

   

    // chapter 7, exercise 1a

    // display contents of stack in reverse, i.e. top last

    public static void displayStackInReverse(Stack aStack) {

        Stack auxStack = new Stack();

       

        // move aStack to auxStack

        while (!aStack.empty()) {

            auxStack.push(aStack.pop());

        }

       

        // move auxStack to aStack, displaying each item

        while (!auxStack.empty()) {

            Object item = auxStack.pop();

            System.out.println(item);

            aStack.push(item);

        }

    }

   

    // chapter 7, exercise 1b

    // count number of items in stack, leaving stack unchanged

    public static int stackSize(Stack aStack) {

        Stack auxStack = new Stack();

       

        // determine size by pushing aStack onto auxStack

        int size = 0;

        while (!aStack.empty()) {

            auxStack.push(aStack.pop());

            size++;

        }

       

        // recover aStack

        while (!auxStack.empty()) {

            aStack.push(auxStack.pop());

        }

       

        // return size of aStack

        return size;

    }

   

    // chapter 7, excercise 1c

    // delete all occurrences of a specified item

    public static void removeStackItem(Stack aStack, Object item) {

        Stack auxStack = new Stack();

       

        // move all items not equal to item to auxStack

        while (!aStack.empty()) {

            Object top = aStack.pop();

            if (!top.equals(item)) {

                auxStack.push(top);

            }

        }

       

        // recovery aStack from auxStack

        while (!auxStack.empty()) {

            aStack.push(auxStack.pop());

        }

    }

   

    public static void main(String[] args) {

        Stack aStack = new Stack();

       

        // test displayStackInReverse

        // note, Dates is on the top of the stack

        aStack.push("Apples");

        aStack.push("Bananas");

        aStack.push("Cherries");

        aStack.push("Cherries");

        aStack.push("Dates");

        displayStackInReverse(aStack);

       

        // Display size of aStack

        System.out.printf("aStack size is: %d\n", stackSize(aStack));

       

        // remove Cherries from aStack and display stack in reverse

        removeStackItem(aStack, "Cherries");

        displayStackInReverse(aStack);

       

    }

   

}

   

Chapter 7, Exercise 2

 

 

 

 

 

 

 

 

 

 


  1. Can pop from aStack and push to bStack
  2. Can pop from cStack and push to bStack
  3. Can pop from bStack and push to aStack
  4. Can pop from bStack and push to cStack

 

Assume that all railroad cars start on track b (bStack). Any permutation of cars can be achieved if we can move a car in position j up to its desired position i < j without disturbing the positions of cars above position i (0 .. i-1). If we have such an algorithm, then any desired permutation can be achieved by looping through positions i = 0 to n –1, where n is the number of cars, and moving the car that is supposed to be in position i up to position i. We can move a car in position j > i up to position i by: 1) push cars in position 0 to j-1 from bStack to aStack; 2) pushing the car in position j from bStack to cStack; 3) pushing cars j-1 to i  from aStack to bStack; 4) pushing the car in cStack to bStack; and 5) pushing the remaining cars on aStack to bStack.

 

Chapter 7, Exercise 3

 

a.

    public static void displayStack(Stack aStack) {

        Stack auxStack = new Stack();

       

        // display contents of aStack, while pushing to auxStack

        while (!aStack.empty()) {

            Object item = aStack.pop();

            System.out.println(item);

            auxStack.push(item);

        }

       

        // push contents of auxStack back onto bStack

        while (!auxStack.empty()) {

            aStack.push(auxStack.pop());

        }

    }

 

  1. See LinkListStack below.

 

 

Chapter 7, Exercise 4

 

package stack;

 

public class LinkListStack {

   

    private class Node {

        Object data;

        Node next;

       

        Node (Object data, Node next) {

            this.data = data;

            this.next = next;

        }

    }

   

    private Node head;

   

    public LinkListStack() {

    }

   

    public boolean isEmpty() {

        return head == null;

    }

   

    public void popAll() {

        head = null;

    }

   

    public void push(Object newItem) throws StackException {

        head = new Node(newItem, head);

    }

   

    public Object pop() throws StackException {

        if (head == null) {

            throw new StackException("Stack is empty");

        }

       

        Object item = head.data;

        head = head.next;

        return item;

    }

   

    public void pop(int n) throws StackException {

        Node temp = head;

        int count = 0;

        while (temp != null && count < n) {

            temp = temp.next;

            count++;

        }

       

        if (temp == null) {

            throw new StackException ("Size of stack < " + n);

        }

       

        head = temp;

    }

   

    public Object peek() throws StackException {

        if (head == null) {

            throw new StackException("Stack is empty");

        }

        return head.data;

    }

   

    public void display() {

        Node temp = head;

        while (temp != null) {

            System.out.println(temp.data);

            temp = temp.next;

        }

    }

   

    public static void main(String[] args) {

        LinkListStack stack = new LinkListStack();

       

        stack.push("Apples");

        stack.push("Bananas");

        stack.push("Cherries");

        stack.push("Dates");

        

        stack.display();

        stack.pop(2);

        System.out.println();

        stack.display();

    }

}