next up previous index
Next: Instance variables and the Up: Data Structures Previous: Extensions of the Iterator   Index


Linked Lists

We have seen the usefulness of linked representations for implementing stacks and queues. We now introduce a fuller implementation of singly linked lists. The class will implement the following methods:
public class LinkedList {
    public boolean isEmpty();
    public int size();
    public void addFirst(Object item);
    public Object firstItem();
    public void removeFirst();
    public boolean contains(Object item);
    public void reverse();
    public Iterator iterator();
    public boolean equals(Object other);
    public String toString();
}

The approach here is deliberately simple. A list will be represented internally by a single ListNode constituting the head of the list. We shall use the device of setting this to be null to indicate an empty list. A more elegant approach to the problem of representing empty structures, but one which is a little less straightforward, will be adopted later when we deal with binary search trees.



Subsections

Peter Williams 2005-06-07