Fundamentals of Computer Science II (CSC-152 99F)


Assignment 4: Linked Lists

Preliminaries

Assigned: Monday, 1 November 1999
Due: 10:00 a.m., Wednesday, 10 November 1999

Summary: In this assignment, you will further ground your understanding of lists and data structures by developing an implementation of lists.

Turning it in: Give me a printed copy of anything you do, including sample output. Also email me the location of your code and share that code.

Collaboration: You may work on this assignment in groups of up to size four. You may also discuss your work with students in other groups and help each other debug your code. However, each groups' code should be substantially their own and you should document any conversations or help you received from others.

Code:

Testing: I will make a test procedure available later this week. My own testing may be more in-depth.

Assignment

Implement linked lists with cursors, according to the CursoredList interface. Make sure to test your implementation.

/**
 * Ordered lists with simple cursors.  The cursor indicates the
 * current element of the list.
 *
 * @author Samuel A. Rebelsky
 * @version 1.1 of November 1999
 */
public interface CursoredList {

  /**
   * Add an element to the end of the list.
   */
  public void addToEnd(Object element);

  /**
   * Add an element to the front of the list.
   */
  public void addToFront(Object element);
 
  /**
   * Add an element after the current element.
   * Precondition: There is a current element (specified by the cursor).
   */
  public void addAfterCursor(Object element);
 
  /**
   * Delete the current element.
   * Pre: There is a current element (specified by the cursor).
   * Post: That element is deleted.
   * Post: The cursor advances to the next element, if there is
   *   a next element.  Otherwise, the position of the cursor
   *   is unknown.
   */
  public void delete();

  /**
   * Move the cursor to the next element in the list equal
   * to the specified element.
   * Pre: There is a current element (specified by the cursor).
   * Post: The cursor is on the first equal element that follows
   *   its previous position, if such an element exists.  If no
   *   such element exists, the position of the cursor is unspecified.
   * @exception
   *   If there are no equal elements after the cursor.
   */
  public void find(Object findMe)
    throws Exception;

  /**
   * Move the cursor to the next element in the list equal
   * to the specified element, using compare for comparisons.
   * Pre: There is a current element (specified by the cursor).
   * Post: The cursor is on the first equal element that follows
   *   its previous position, if such an element exists.  If no
   *   such element exists, the position of the cursor is unspecified.
   * @exception
   *   If there are no equal elements after the cursor.
   */
  public void find(Object findMe, Comparator compare)
    throws Exception;

  /**
   * Move the cursor to the front of the list.
   * Pre: The list is nonempty.
   * Post: The cursor is at the front of the list.
   */
  public void front();

  /** 
   * Get the current element of the list.
   * Pre: There is a current element (specified by the cursor).
   */
  public Object getCurrent();

  /**
   * Advance the cursor to the next elmeent.
   * Pre: The current element is not the last element.
   * Pre: There is a current element (specified by the cursor).
   * Post: The cursor advances to the next element, if there is
   *   a next element.  If there is no next element, the
   *   position of the cursor is unknown.
   * @exception Exception
   *   If there is no next element.
   */
  public void advance()
    throws Exception;

} // interface CursoredList

Your linked lists should use nodes. You may use my Node class. You may also design your own nodes (including doubly-linked nodes, if you decide that they're easier to use).

/**
 * A node in a linked list along with some associated utilities.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of November 1999
 */
public class Node {
  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+

  /** The contents of the node. */
  protected Object contents;

  /** The next node in the list. */
  protected Node next;

  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+

  /** Build a new node with specified contents and next. */
  public Node(Object contents, Node next) {
    this.contents = contents;
    this.next = next;
  } // Node(Object, Node)

  // +-----------------------------------------------------------
  // | Extractors |
  // +------------+

  /**
   * Get the contents of the node.
   * Pre: The node is initialized and nonempty.
   * Post: Returns the contents of the current node.
   * Post: Does not modify the node.
   */
  public Object getContents() {
    return this.contents;
  } // getContents()

  /**
   * Get the next node in the list.
   * Pre: The list is initialized and nonempty.
   * Post: Returns the next node in the list.
   * Post: Does not modify the node.
   */
  public Node getNext() {
    return this.next;
  } // getNext()

  /**
   * Determine if the list represented by a node is empty.
   * We use null to represent the empty list.
   * Pre: (none)
   * Post: Returns true if the parameter is the empty list
   *   and false otherwise.
   */
  public static boolean isEmpty(Node checkMe) {
    return (checkMe == null);
  } // isEmpty(checkMe)


  // +-----------+-----------------------------------------------
  // | Modifiers |
  // +-----------+

  /**
   * Set the contents of this node to newContents.
   * Pre: The node is initialized.
   * Post: Subsequent calls to getContents return newContents (until the
   *   contents is updated again).
   * Post: The rest of the list is not modified.
   */
  public void setContents(Object newContents) {
    this.contents = newContents;
  } // setHead(Object)

  /**
   * Set the next node in the list to newNext.
   * Pre: The node is initialized.
   * Post: Subsequent calls to getNext return newNext (until the
   *   next element is updated again).
   * Post: newNext is not modified.
   */
  public void setNext(Node newNext) {
    this.next = newNext;
  } // setTail(Node)

  /**
   * Create an empty list.
   */
  public static Node nil() {
    return null;
  } // nil()

  /**
   * Cons a value onto the front of a list.
   */
  public static Node cons(Object contents, Node next) {
    return new Node(contents, next);
  } // cons(Object, Node)

} // class Next

Extra Credit

Implement bidirectional cursored lists, which add the following methods:

You are responsible for writing the specifications of these methods.

History

Monday, 1 November 1999

Wednesday, 3 November 1999

Tuesday, 9 November 1999


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Assignments/assign.04.html

Source text last modified Tue Nov 9 16:05:01 1999.

This page generated on Thu Nov 18 21:54:21 1999 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu