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


Class 32: Implementations of Lists

Back to Discussion of Exam 2. On to Vectors.

Held Wednesday, October 27, 1999

Overview

Today, we will continue our discussion of lists by considering techniques one might use for implementing lists.

Notes

Contents

Handouts

Summary


The Design of Lists, Revisited

Review

Three Kinds of Lists

Implementing Lists

Implementing Lists with Arrays

/**
 * The start of an implementation of Simple Lists, using arrays
 * as the underlying implementation structure.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of March 1999
 */
public class ArrayBasedSimpleList
  implements SimpleList
{
  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+

  /** The elements of the list. */
  protected Object[] elements;

  /** The cursor. */
  protected int cursor;

  /** 
   * The length of the list (different from the length of
   * the array).  Also used as the index of the next element
   * to add.
   */
  protected int length;

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

  /** Create a new list of specified capacity. */
  public ArrayBasedSimpleList(int capacity) {
    elements = new Object[capacity];
    reset();
  } // ArrayBasedSimpleList(int)

  // +---------+-------------------------------------------------
  // | Methods |
  // +---------+

  /** 
   * Add an element to the list.  See the interface for
   * preconditions and postconditions.
   */
  public void add(Object element) {
    // Add the element.
    this.elements[this.length] = element;
    // Increase the length
    ++this.length;
  } // add(Object)

  /**
   * Delete an element from the list.  See the interface for
   * preconditions and postconditions.
   */
  public void delete(Object element) {
    // Step through the list until we find an equal element
    for (int i = 0; i < this.length; ++i) {
      // Note that we use element.equal because some
      // elements of the list may be null
      if (element.equals(this.elements[i])) {
        // Found it!  
        // Put the last thing here.
        this.elements[i] = this.elements[this.length-1];
        // Clear the last thing.
        this.elements[this.length-1] = null;
        // Update the length.
        --this.length;
        // And we're done.
        return;
      }
    } // for
    // Nope, didn't find it.  Nothing else to do.
  } // delete(Object)

  /**
   * Determine the length of the list.  See the interface
   * for preconditions and postconditions.
   */
  public int length() {
    return this.length;
  } // length()

  /**
   * Determine whether there is space available.  See the
   * interface for preconditions and postconditions.
   */
  public boolean spaceAvailable() {
    // There is space available if the length of the list is
    // less than the length of the array.
    return this.length < this.elements.length;
  } // spaceAvailable()

  /**
   * Reset iteration.  See the interface for preconditions and
   * postconditions.
   */
  public void reset() {
    this.cursor = 0;
  } // reset()

  /**
   * Get the next element.  See the interface for preconditions
   * and postconditions.
   */
  public Object nextElement() {
    // Note that it is possible to express this more concisely as
    //   return this.elements[this.cursor++];

    // Get the element to return.
    Object tmp = this.elements[this.cursor];
    // Advance the cursor.
    ++this.cursor;
    // Return the element.
    return tmp;
  } // nextElement()

  /**
   * Are there more elements left?  See the interface for preconditions
   * and postconditions.
   */
  public boolean hasMoreElements() {
    return this.cursor <= this.length;
  } // hasMoreElements()
} // class ArrayBasedSimpleList

Resizing Array-Based Lists

Gaps in the Array

Implementing Ordered Lists

Implementing Lists with Cons Cells


History

Tuesday, 10 August 1999

Wednesday, 27 October 1999

Back to Discussion of Exam 2. On to Vectors.


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/Outlines/outline.32.html

Source text last modified Wed Oct 27 12:17:52 1999.

This page generated on Thu Oct 28 09:51:04 1999 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu