CS 231: Lab #3

Linked Lists

The goal of this lab period is to get you started on the current assignment. We'll go over a singly-linked list, private classes, Iterators, and how to create a generic container.

Documentation for Java 1.6 is located at: Java 1.6 SE API


Tasks

  1. Create a new java file called LinkedList.java. Import java.util.*. Define the class as:

    public class LinkedList<T>

  2. First, we need to create a container class that will hold a generic object and a next pointer. Make a private class called Node that has a Node field called next, and a T field with a name of your choice. The Node class should have the following methods.
    • public Node(T item) - a constructor that initialized 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() - reutrns the next field.
  3. Create the fields for the LinkedList class to store the head of the list and the number of items in the list. Then make the following methods for the LinkedList class.
    • public LinkedList() - constructor that returns an empty list.
    • public void clear() - empties the list.
    • public int size() - returns the size of the list.
    • public void add( T item ) - adds item to the head of the list.
  4. To enable another program to loop through our list, we're going to implement the Iterable interface, which allows a programmer to use the 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 to your LinkedList class. It should return a new LLIterator with head passed to the constructor.

    public Iterator iterator()

  5. Now copy the following main function to your LinkedList.java file and test out your Iterator.
    public static void main(String[] argv) {
        LinkedList<Integer> a = new LinkedList<Integer>();
    
    	a.add(5);
    	a.add(10);
    	a.add(20);
    
    	System.out.printf("\nAfter setup %d\n", a.size());
    	for(Integer item: a) {
    		System.out.printf("thing %d\n", item);
    	}
    	a.clear();
    
    	System.out.printf("\nAfter clearing %d\n", a.size());
    	for(Integer item: a) {
    		System.out.printf("thing %d\n", item);
    	}
    
            for(int i=0;i<20;i+=2) {
    		a.add( i );
    	}
    
    	System.out.printf("\nAfter setting %d\n", a.size());
    	for(Integer item: a) {
    		System.out.printf("thing %d\n", item);
    	}
    }
    
  6. 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 your main test function.

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

Once you are comfortable with the above tasks, go on to the assignment.