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 04

Linked Lists and Amortization

Contents:


Overview

Topic: Linked Lists, Amortization
Related Reading: Ch. 4.4, 4.5, and 5.1
Due: 9:30am Thursday, 21 October 2004

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


Practice Problems


Problems to be Submitted (20 points)

  1. (5 points)

    Creativity Exercise C-4.10 of the text.
    NOTE: Please give a piece of C++ code as your solution. Your method should be defined in the spirit of,

       Node concatenate(Node headerL, Node headerM) {
    where the node returned is the header of the resulting list L'. In creating the new list, you are free to damage or destroy the original lists if you wish. Please remember to report the running time!

    A further hint can be found through Hint Server on textbook's website.

  2. (5 points)

    Creativity Exercise C-4.11 of the text.
    NOTE: Please give a piece of C++ code as your solution. Your method should be defined in the spirit of,

       Node concatenate(Node headerL, Node, trailerL, Node
                        headerM, Node trailerM) {
    where the node returned is the header of the resulting list L'. In creating the new list, you are free to damage or destroy the original lists if you wish. Please remember to report the running time!

  3. (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());
      }
    
    }

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.


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

Last modified: Tuesday, 26 October 2004
Class Photo | Course Home | Homework | Programming | Schedule & Lecture Notes | Submit