Homework Assignment

Stacks and Queues

Contents:

  • Overview
  • Practice Problems
  • Problems to be Submitted
  • Extra Credit

  • Overview

    Topic: Stacks and Queues
    Related Reading: Ch. 1, 2, 4.1, 4.2 (skim 4.2.4)
    Due:

    Please make sure you adhere to the policies on academic honesty.


    Practice Problems

  • Reinforcement Exercise R-4.1 on p. 179.
  • Reinforcement Exercise R-4.2 on p. 179.

  • Problems to be Submitted (20 points)

    Note: Problems A and B should be relatively straightforward if you are careful. Problem C is quite challenging, but a great exercise.

    1. (5 points)
      Using the convention of Example 4.3 of the text (p. 138), show the effects of the following series of stack operations on an initially empty stack S of integer objects: push(10), pop(), push(3), push(5), push(1), pop(), isEmpty(), size(), top(), push(8), pop(), pop().

    2. (5 points)
      Using the convention of Example 4.4 of the text (p. 150), show the effects of the following series of queue operations on an initially empty queue Q of integer objects: enqueue(2), enqueue(4), size(), front(), enqueue(6), dequeue(), enqueue(3), dequeue(), dequeue(), enqueue(7), front().

    3. (10 points)
      Describe how to implement the queue ADT using two stacks. What is the running time of the enqueue() and dequeue() methods?

      We have seen the interface Stack (Code Fragment 4.1) and the interface Queue (Code Fragment 4.8), both of which are assumed to have unlimited capacity. For this problem you are to assume that someone has already implemented the Stack interface (but you should not necessarily assume that they have used the text's array-based implementation).

      We want you to give a Java class which implements the Queue interface, using only two Stack's as instance variables (you are free to define local variables within particular methods if you wish).

      Your code should start with the lines,

      public class SolutionQueue implements Queue {
        private Stack S1 = new AnImplementedStack();
        private Stack S2 = new AnImplementedStack();
      
      

      and should go on to contain code for the methods enqueue(o), dequeue(), size(), isEmpty(), and front(). To be successful, you will need to have a good understanding of both sides of "the wall" defined by an interface. You are simultaneously in the role of the implementor of a Queue, yet the user of someone else's Stack implementation.

      To get you going, a natural implementation for the Queue.size() method in this setting might be:

      public int size() {   // returns the size of the Queue
        return (S1.size() + S2.size());
      }
      since all elements of the queue must be stored, and S1 and S2 are your only means of storage. (of course, if you decide to store an element in both stacks for some reason, you may have to adjust this size() implementation)

      If you understand the goal, it is now time to think about how to solve the problem. Remember, anytime dequeue() is called, your Queue must behave with a "first-in, first-out" principle. Make sure your code works for any sequence of enqueue(), dequeue(), and front() operations


    Extra Credit

    1. (2 points)
      Describe how to implement the stack ADT using two queues.


    Last modified: 6 September 2002