CS 201 - Lab 7

Lists and Iterators

In this lab you will complete two different implementations of a generic list data collection class.  A list is a collection which stores its elements in a sequence.

We saw in Lab 5 how to implement a generic list using an underlying storage array, which is reallocated as needed as elements are added to the collection.  In this lab, you will complete an implementation of an array-based list class called YCPArrayList.  You will also implement (from scratch) a class called YCPLinkedList, which (as the name suggests) is implemented using an underlying linked list data structure.

Getting Started

Import lab7.zip.  (File->Import...->General->Existing Projects into Workspace->Archive File).  You should see a project called lab7 in the Package Explorer.

Your task

Both the YCPArrayList and the YCPLinkedList classes implement the YCPList interface, which defines the operations which list classes must support.  This interface is defined as follows:

public interface YCPList<E> {
public int size();
public void append(E element);
public void prepend(E element);
public E get(int index);
public void remove(int index);
public Iterator<E> iterator();

The size method returns the number of elements which have been added to the list.

The append and prepend methods add a new element value to the end and beginning of the list, respectively.

The get method returns the element value at the specified index; 0 for the first element, 1 for the second, etc.

The remove method removes the element at the specified index; 0 for the first element, 1 for the second, etc.

The iterator method returns an Iterator object positioned just before the first element of the collection.  An Iterator object is an instance of a class which implements the java.util.Iterator interface.  Iterator objects support methods to traverse the elements of the collection, and to remove the element which has just been visited by the iterator.  (Follow the link to view the API documentation for the interface.)

Your task is to implement all of the YCPList methods for both the YCPArrayList and the YCPLinkedList classes.

NOTE: there probably will not be enough time to do this lab entirely in class.  However, you should be able to make significant progress.

The YCPArrayListTest and YCPLinkedListTest classes contain unit tests to test the YCPArrayList and YCPLinkedList classes, respectively.  You are done when all unit tests pass.


Start out by implementing the unimplemented methods (remove and iterator) in YCPArrayList.  You will need to write an custom Iterator class.

Next, start working on the YCPLinkedList class.  Start with the size, append, and get methods.  You will need to create a class to represent the nodes of the linked list.  Eventually, when you implement the iterator method, you will need to add an Iterator class.

Linked lists are tricky to implement correctly!  You will need to think carefully about how each method works.  Draw box-and-arrow diagrams to plan out how each method should modify the linked list structure.
Designing an Iterator class for a linked list that supports the remove operation is quite tricky.  Start by designing an Iterator class that supports just the hasNext and next operations, and then try to extend it to support remove.