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


Assignment Five: Array-based Lists

Summary: In this assignment, you will build an implementation of the rebelsky.util.List interface. Instead of linked lists, you will base your lists on arrays.

Collaboration: You can work on this assignment in groups of up to size four, with three being an "optimal" size. You may discuss your design with any size group. You may also work with each other on general debugging issues.

If you work as part of a group, you are responsible for ensuring that all members of the group understand all sections of the assignment. You may (and should), however, break up the initial work.

Array-Based Lists

As we suggested earlier in the term, it is possible to efficiently implement bounded-sized lists using arrays. (In fact, if you're willing to allocate new arrays the list size does not need to be bounded.) We might call this array list_array and declare it with

Object[] list_array;

In a naive implementation, the first element of the list is in the 0th element of the array. An integer value, back, keeps track of the index of the last element. If back is -1 then the array is empty. In the naive implementation, addAtEnd might be written as

  /**
   * Add an element at the end of the list.  If the list is
   * empty, creates a one-element list.
   * <br><strong>Precondition:</strong> 
   *   The list has sufficient capacity to include the new element.
   * <br><strong>Postcondition:</strong>
   *   The element is now at the end of the list.
   * <br><strong>Postcondition:</strong>
   *   The list increases in length by one.
   * <br><strong>Postcondition:</strong>
   *   If the list was not previously empty, the cursor is
   *   unchanged (if it previously referred to the end of the list,
   *   it now refers to the penulitimate element of the list).
   * <br><strong>Postcondition:</strong>
   *   If the list was previously empty, the list now has one element  
   *   which is front, back, and cursor.
   *
   * @throw ListException
   *   If the list is full.
   */
  public void addAtEnd(Object elt)
  {
    // Make sure the list is not full
    if (back == list_array.length - 1) {
      throw new ListException("No room to add an element");
    }
    // Update back
    back = back + 1;
    // Add the element
    list_array[back] = elt;
  } // addAtEnd(Object)

A disadvantage of the naive implementation is that insertion and deletion at the front are expensive. Hence, it is often useful to use a slightly different implementation. We'll use a second integer value, front to indicate the front of the list.

For example, consider the list created by

addAtEnd("A");
addAtEnd("B");
addAtEnd("C");

In the array representation of this list, front is 0 and back is 2. If we then delete the first element, we change front to 1 instead of shifting the array left and decrementing back.

A possible problem occurs when you try to addAtEnd("X") and back is one less than the length of the array. Since the first element is not necessarily the 0th element of the array, you may be able to employ wraparound, setting back to 0.

Assignment

Your goal is to develop a class, ArrayBasedList, that implements all of the methods in the rebelsky.util.List interface. Your class should use arrays to implement the lists and ensure that all of the non-cursor-related operations take constant time. (Some of the cursor-related operations, such as insertAfterCurrent, may take linear time.)

You should include at least one constructor, ArrayBasedList(int capacity) that creates an array-based-list that can hold at least capacity elements.

You should also include an Object elementAt(int pos) method that returns the object at the appropriate position of the list (which is not necessarily the same as the position within the array).

It is important that you use this naming strategy, as I'll be using a standard testing routine (which I will distribute later this week).

Testing

When turning in your assignment, make sure to include tests that demonstrate that your various functions work correctly in all situations.

Analysis

Include a short running time analysis of each method you develop. As suggested above, m ost of your methods should be O(1).


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.

Source text last modified Fri Apr 3 07:27:24 1998.

This page generated on Tue Jan 12 11:44:35 1999 by SiteWeaver.

Contact our webmaster at rebelsky@math.grin.edu