Homework Assignment

Linked Lists and Amortization

Contents:

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

  • Overview

    Topic: Linked Lists and Amortization
    Related Reading: Ch. 4.3, 4.4, and 5.1
    Due:

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


    Practice Problems

  • If you wish, you can play with the singly-linked list demonstration Applet. However this applet requires a browser with a relatively recent version of the Java VM, or else requires the download of such a Java plugin.
  • Creativity Exercise C-4.1 of the text (or related lab)
  • Reinforcement Exercise R-5.3 of the text.

  • Problems to be Submitted (20 points)

    1. (5 points)
      Creativity Exercise C-4.3 of the text.

      NOTE: Please give a piece of Java code as your solution. Your method should be defined in the spirit of,

         Node concatenate(Node headerL, Node headerM) {
      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.4 of the text.

      NOTE: Please give a piece of Java code as your solution. Your method should be defined in the spirit of,

         DLNode concatenate(DLNode headerL, DLNode trailerL,
                            DLNode headerM, DLNode trailerM) {
      where the node returned is the header of the resulting list. 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 Java 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.

      public class QueueImplementation implements Queue {
        private Stack S1 = new StackImplementation();
        private Stack S2 = new StackImplementation();
      
        public int size() { return (S1.size() + S2.size()); }
      
        public boolean isEmpty() { return (S1.isEmpty() && S2.isEmpty()); }
      
        public void enqueue(Object obj) {
          S1.push(obj);
        }
      
        public Object dequeue() throws QueueEmptyException {
          if (S2.isEmpty()) {
            if (S1.isEmpty()) {
              throw new QueueEmptyException("Queue is empty.");
            } else {
              // move items from S1 onto S2
              while (S1.size()>0) {
                S2.push(S1.pop());
              }
            }
          }
          return(S2.pop());
        }
      
        public Object front() throws QueueEmptyException {
          if (S2.isEmpty()) {
            if (S1.isEmpty()) {
              throw new QueueEmptyException("Queue is empty.");
            } else {
              // move items from S1 onto S2
              while (S1.size()>0) {
                S2.push(S1.pop());
              }
            }
          }
          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.


    Last modified: 6 September 2002