Fundamentals of Computer Science II (CSC-152 2000S)


Class 31: Implementing Lists

Back to Introduction to Lists. On to Lab: Animation.

Held Wednesday, March 15, 2000

Overview

Today we consider a variety of issues pertaining to lists.

Notes

Contents

Summary


Three Kinds of Lists

Implementing Scheme Lists in Java

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

History

Tuesday, 18 January 2000

Wednesday, 15 March 2000

Wednesday, 5 April 2000

Back to Introduction to Lists. On to Lab: Animation.


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/2000S/Outlines/outline.31.html

Source text last modified Wed Apr 5 09:05:46 2000.

This page generated on Wed Apr 5 09:53:43 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu