// CSE 143, Summer 2012 // 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; private int size; // Constructs an empty list public LinkedList () { front = null; size = 0; } // Returns the value at a given index. // pre: 0 <= index < size // Throws a NullPointerException if index > size or index < 0. public E get (int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("index was out of bounds"); } ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } // Adds a value at a given index. // Pre: 0 <= index <= size // Throws a NullPointerException if index > size or index < 0 public void add (int index, E value) { if (index < 0 || index > size) { throw new IllegalArgumentException("index was out of bounds"); } else 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); } size++; } // Removes the value at the given index. public E remove() { if (front == null) { throw new NoSuchElementException(); } size--; E temp = front.data; front = front.next; return temp; } // Removes the value at the given index. // Pre: 0 <= index < size public void remove (int index) { if (index < 0 || index >= size) { throw new NoSuchElementException(); } else if (index == 0) { remove(); } else { ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; size--; } } // returns the number of elements in the list public int size() { return size; } // returns a string representation of the data in the list // surrounded by square brackets and separated by commas public String toString () { String str = "["; if (front != null) { str += front.data; ListNode current = front.next; while (current != null) { str += ", " + current.data; current = current.next; } } str += "]"; return str; } // 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; } public Iterator iterator() { return new LinkedListIterator(); } // A ListNode object represents a single node in a linked list. // A node stores an integer value and a link to the next node in the list. private class ListNode { public E data; public ListNode next; public ListNode(E data) { this.data = data; } public ListNode (E data, ListNode next) { this.data = data; this.next = next; } } // This inner class implements an Iterator over the elements of this list. // Not perfect; doesn't support remove. private class LinkedListIterator implements Iterator { private ListNode current; // current position in list public LinkedListIterator() { current = front; } public boolean hasNext() { return current != null; } public E next() { E result = current.data; current = current.next; return result; } // not implemented for now public void remove() { throw new UnsupportedOperationException(); } } }