Class Photo | Course Home | Homework | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science 220
Computer Science II
Michael Goldwasser

Fall 2004

Dept. of Math & Computer Science

Homework Assignment 02

Stacks and Queues

Contents:


Overview

Topic: Stacks and Queues
Related Reading: Ch. 4.2-4.3
Due: 9:30am, Thursday, 16 September 2004

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


Practice Problems


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.6 of the text (p. 157), show the effects of the following series of stack operations on an initially empty stack S of integers:
    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.7 of the text (p. 170), show the effects of the following series of queue operations on an initially empty queue Q of integers:
    enqueue(2)
    enqueue(4)
    size()
    front()
    enqueue(6)
    dequeue()
    enqueue(3)
    dequeue()
    dequeue()
    enqueue(7)
    front()

  3. (10 points)

    In this problem, we wish to have you demonstrate a general software design technique, known as an adapter pattern. This is when you implement a given class based on the use of some other existing class. For this problem, we ask that you show how to implement the Queue ADT, using two stacks as your only instance variables!

    Specifically, you should presume that someone else has already written code to implement the Stack implementation, as outlined in Code Fragment 4.9. Due to encapsulation, you should make no assumptions about the details of that implementation; in particular, do not assume that they used the ArrayStack impelmentation of the text.

    Now it is your turn. Show how to implement the Queue ADT, as outlined in Code Fragment 4.16. For concreteness, we ask that you write your answer to this problem in C++ syntax. Your code should be based on the following outline:

    template 
    class QueueSolution {
    private:
      // Do not use any data members other than the following two stacks
      StackImplementation<Object> S1;
      StackImplementation<Object> S2;
    
    public:
       bool isEmpty() const {
    
    
       }
    
      int size() const {
    
       }
    
    
       Object& front() throw(QueueEmptyException) {
    
       }
    
    
       void enqueue(const Object& obj) {
    
       }
    
       Object dequeue() throw(QueueEmptyException) {
    
       }
    }
    

    To be successfully, you will need to keep straight your role as the implementor of a Queue, yet also your role as a user of someone else's Stack implementation.

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

      int size() const {
         return (S1.size() + S2.size());
       }
    
    since we presume that 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)

    As a dual to Problem C, demonstrate an adapter pattern which would implement the Stack interface, using two queues as the only instance variables.


CSA-220, Fall 2004
Michael Goldwasser
goldwamh at our university domain.

Last modified: Wednesday, 08 September 2004
Class Photo | Course Home | Homework | Programming | Schedule & Lecture Notes | Submit