// Allison Obourn // CSE 143 - lecture 22 // A LinkedList object stores a list of values using // a linked list as its underlying data structure. // This version uses "generics" so that it can store any type of value, not // just integers. It also implements the same interface as ArrayList, // so that the two can be used interchangeably in client code. import java.util.*; public class LinkedList extends AbstractList { private ListNode front; // refers to first node in list (null if empty) // Constructs a new empty list. public LinkedList() { front = null; } // Adds a value at a given index. // pre: 0 <= index <= size public void add(int index, E value) { if (index == 0) { front = new ListNode(value, front); } else { ListNode current = front; for(int i = 0; i < index - 1; i++) { current = current.next; } current.next = new ListNode(value, current.next); } } // removes and returns the first element in the list // Pre: size > 0 public E remove() { if (front == null) { throw new NoSuchElementException(); } E value = front.data; front = front.next; return value; } // Removes the value at the given index. // Pre: 0 <= index < size public void remove(int index) { if (index == 0) { front = front.next; } else { ListNode current = front; for(int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; } } // Returns the element at the specified index from the list. // Precondition: 0 <= index < size public E get(int index) { ListNode current = front; for(int i = 0; i < index; i++) { current = current.next; } return current.data; } // Sets the element at the specified index in the list to have the given value. // Precondition: 0 <= index < size // Throws a NullPointerException if index >= size. public void set(int index, E value) { ListNode current = goTo(index); current.data = value; } // Returns a comma-separated String representation of this list. public String toString() { if (front == null) { return "[]"; } else { String result = "[" + front.data; ListNode current = front.next; while (current != null) { result += ", " + current.data; current = current.next; } result += "]"; return result; } } // Returns the number of elements in this list. public int size() { int count = 0; ListNode current = front; while (current != null) { current = current.next; count++; } return count; } // Returns the index of the first occurrence of the given value in the list, // or -1 if the value is not found in the list. public int indexOf(E value) { int index = 0; ListNode current = front; while (current != null) { if (current.data.equals(value)) { return index; } index++; current = current.next; } return -1; } // returns an iterator over this LinkedList object public Iterator iterator() { return new LinkedIterator(); } // Returns a reference to the node object representing the index'th element // in the list. Used as a helper by many of the public methods. private ListNode goTo(int index) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current; } // A ListNode represents a single node in a linked list. It stores an integer // value and a link to the next node. private class ListNode { private E data; private ListNode next; // Creates a terminal ListNode with the specified integer data. public ListNode(E data) { this(data, null); } // Creates a ListNode with the specified integer data and next node. public ListNode(E data, ListNode next) { this.data = data; this.next = next; } } // A LinkedIterator is an iterator for the outer LinkedList private class LinkedIterator implements Iterator { private ListNode current; // current position in list public LinkedIterator() { current = front; } public boolean hasNext() { return current != null; } public E next() { E result = current.data; current = current.next; return result; } public void remove() { // not implemented for now throw new UnsupportedOperationException(); } } }