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

Saint Louis University

Computer Science A220/P126
Data Structures and Object-Oriented Programming

Michael Goldwasser

Spring 2005

Dept. of Math & Computer Science

Homework Assignment 06

Amortization, Vectors and Lists

Contents:


Overview

Topic: Amortization, Vectors and Lists
Related Reading: Ch. 5.1-5.2
Due: Wednesday, 23 March 2005, 1:10pm

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


Practice Problems


Problems to be Submitted (20 points)

  1. (10 points)

    In the solutions of an earlier homework, we gave a method for implementing a Queue with the use of two Stack data structures. Although this method was relatively simple to understand, it was not very efficient. This is true both in terms of worst-case analysis of the methods and even amortized analysis.

    So in a moment, we are going to design a new implementation for a Queue, which again is based on using two Stack's. Although the worst-case time per operation is still not O(1) (as it would be when we implement a Queue directly with an array or a linked list), it turns out that we can show that each operation requires only O(1) amortized time.

    Your job is to prove this amortized bound using the accounting method of the text. Specifically, you should imagine yourself in the role of the Queue implementor. The person who has implemented the Stack has demanded that you pay $1 cyber-dollar every time your code calls one of the Stack methods.

    The remaining text gives the C++ code for the new approach. Whereas the original solution used stack S2 only temporarily to hold objects, this new method uses both stacks S1 and S2 for storing objects. Although you are not required to argue the correctness of this new method, you will certainly need to understand how and why it works to complete this assignment.

    class QueueSolution : Queue {
    private:
      // Do not use any data members other than the following two stacks
      StackImplementation<Object> S1;
      StackImplementation<Object> S2;
    
      void transferItems() throw(QueueEmptyException) {
        // assuming S2 is empty and S1 is non-empty
        // transfer all items from S1 onto S2
        int s = S1.size();
        if (s==0)
          throw QueueEmptyException("Queue is empty.");
        for (int i=0; i<s; i++) {
          S2.push(S1.pop());
        }
      }
          
    public:
      bool isEmpty() const { return S1.isEmpty() && S2.isEmpty();  }
    
      int size() const { return S1.size()+S2.size();  }
    
      void enqueue(const Object& obj) { S1.push(obj); }
    
      Object dequeue() throw(QueueEmptyException) {
        if (S2.isEmpty())
          transferItems();
        return(S2.pop());
      }
    
      Object& front() throw(QueueEmptyException) {
        if (S2.isEmpty())
          transferItems();
        return(S2.top());
      }
    
    }
  2. (10 points)

    Design algorithms for reversing a List using only a restricted subset of the functions, as indicated below. Each algorithm should rearrange the elements of the List. Returing a new List is not allowed, although other lists may be used for auxiliary storage.

    1. Reverse a list using only the functions size(), first(), last(), remove() and insertFirst().
    2. Reverse a list using only the functions size(), first(), remove() and insertFirst().

Extra Credit

  1. (2 points)

    Creativity Exercise C-5.4 of the text.
    (Hint available through Hint Server on textbook's website).

    Note: Though you will need to understand Exercise C-5.3, you do not need to writeup that solution.


Michael Goldwasser
CS A220/P126, Spring 2005
Last modified: Monday, 28 March 2005
Course Home | Homework | Programming | Schedule & Lecture Notes | Submit