Linked Lists

The goal of this lab period is to give you an opportunity to implement a generic singly-linked list with an Iterator.

You are expected to implement the Linked List without importing any packages beside the Iterator interface mentioned below.


  1. Setup

    Make a new folder for this week's project.

    Download the LinkedListTests file. This is your copy to edit. Comment out the tests for methods you haven't written yet and use it to test your code as you write it!

    Create a new java file called Import a necessary package:

    import java.util.Iterator;    // defines the Iterator interface
  2. Define the LinkedList class

    Define a public class LinkedList that has a generic type specifier. The type specifier indicates what type of class is stored in the linked list. If no specifier is provided in the declaration, the defualt type is Object.

    public class LinkedList<T> {
  3. Create a container class (a Node)

    A Node needs to hold a generic object and a next pointer. Make a private class inside the LinkedList class called Node that has a field of type Node called next, and a field of type T with a name of your choice. The Node class should have the following methods.

    • public Node(T item) a constructor that initializes next to null and the container field to item.
    • public T getData() returns the value of the container field.
    • public void setNext(Node n) sets next to the given node.
    • public Node getNext() returns the next field.

  4. Create fields and methods for the LinkedList class

    The LinkedList needs to store the head of the list and the number of items in the list. In addition to those fields, the LinkedList class should support the following methods (we recommend working on them in this order).

    • public LinkedList() constructor that initializes the fields so it is an empty list.
    • public int size() returns the size of the list.
    • public void clear() empties the list (resets the fields so it is an empty list).M
    • public boolean isEmpty() returns true if the list is empty, otherwise this method returns false.
    • public String toString() returns a String representation of the list.
    • public void add(T item) inserts the item at the beginning of the list.
    • public boolean contains(Object o) returns true if o is present in this list, otherwise this method returns false. More specifically, if there is an item item in this list such that item.equals(o), then this method should return true.
    • public T get(int index) returns the item specified by the given index.
    • Lab checkpoint: Congrats, you've written your add, get and contains method! Call us over and show us they work by running the LabLinkedListTests file (But also please stick around and finish the lab if you're able to!)
    • public T remove() removes the first item of the list and returns it.
    • public void add(int index, T item) inserts the item at the specified position in the list.
    • public T remove(int index) removes the item at the specified position in the list and returns it.
    • public boolean equals(Object o) returns true if o is a LinkedList that also contains the same items in the same order.

      Notice that for this method the parameter is of type Object instead of being a LinkedList itself. This was done purposefully, to comply with Java's standard equals method, which all Objects technically support. For this method, I recommend first checking if the parameter is itself a LinkedList; if it is not, then you can immediately return false. You can do so by creating an if statement like:

      if (!(o instanceof LinkedList)){
          return false;
      // If I have reached this line, o must be a LinkedList
      LinkedList oAsALinkedList = (LinkedList) o;
      // Now I have a reference to something Java knows is a LinkedList!

When you are done, you can proceed to the project.