CS 231: Lab #4

Title image Spring 2018


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


  1. Make a new folder for this week's project.
  2. Create a new java file called LinkedList.java. Import necessary packages:

    import java.util.Iterator;
    import java.util.ArrayList;
    import java.util.Collections;

    Define the class as:

    public class LinkedList<T>
  3. First, we need to create a container class that will hold a generic object and a next pointer. Make a private class inside your LinkedList class calledNode that has a field of type Node called next, and a field of typeT 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 getThing() 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 for the LinkedList class to store the head of the list and the number of items in the list. Your LinkedList class should support the following methods.

    It's probably worth implementing the first four methods, up though addFirst, then work on your Iterator. You can use this test file to test those four functions and your Iterator.

    • public LinkedList() constructor that initializes the fields so it is an empty list.
    • public void clear() empties the list (resets the fields so it is an empty list).
    • public int size() returns the size of the list.
    • public void addFirst(T item) inserts the item at the beginning of the list.
    • public void addLast(T item) appends the item at the end of the list.
    • public void add(int index, T item) inserts the item at the specified poistion in the list.
    • public T remove (int index) removes the item at the specified position in the list.

  5. To enable another program to loop through our list, we're going to implement the Iterable interface, which allows a programmer to use a foreach loop structure on our LinkedLists. First, change the class definition for LinkedList to be:
    public class LinkedList<T> implements Iterable<T>

    Then we need to create our own Iterator subclass that handles traversing the list. Create a new private class inside the LinkedList class with the following definition.

    private class LLIterator implements Iterator<T>

    Give the class one field, which is of type Node, and which will hold the next node to be provided during a traversal. Then implement the following methods in the class.

    • public LLIterator(Node head) creates an LLIterator given the head of a list.
    • public boolean hasNext() returns true if there are still values to traverse (if the current node reference is not null).
    • public T next() returns the next item in the list, which is the item contained in the current node. The method also needs to move the traversal along to the next node in the list.
    • public void remove() does nothing. Implementing this function is optional for an Iterator.

    Finally, add the function iterator public Iterator iterator() to your LinkedList class. It should return a new LLIterator with head passed to the constructor, shown as the following code:

    // Return a new LLIterator pointing to the head of the list
    public Iterator<T> iterator() {
        return new LLIterator( this.head );
    This method makes your class actually implement Iterable.

  6. Download the LLTest file and test your standard LinkedList functions. Compile and run the test code. If your LinkedList is working properly, it should produce this output. Debug your code until it works.
  7. Create two more methods for your LinkedList class. You can make use of the foreach structure in the first method.
    • public ArrayList<T> toArrayList() returns an ArrayList of the list contents in order.
    • public ArrayList<T> toShuffledList() returns an ArrayList of the list contents in shuffled order.

    Then add the following to the end of the LLTest main function.

    	ArrayList<Integer> alist = llist.toArrayList();
    	System.out.printf("\nAfter copying %d\n", alist.size());
    	for(Integer item: alist) {
    		System.out.printf("thing %d\n", item);
    	alist = llist.toShuffledList();	
    	System.out.printf("\nAfter copying %d\n", alist.size());
    	for(Integer item: alist) {
    		System.out.printf("thing %d\n", item);

When you are done, you can proceed to the project