import java.util.NoSuchElementException; public class LinkedList implements List { private static class Node { private T value; private Node prev; private Node next; private Node(T value, Node prev, Node next) { this.value = value; this.prev = prev; this.next = next; } } private Node head; private int size; public LinkedList() { head = new Node(null, null, null); head.prev = head.next = head; size = 0; } private class LinkedListIterator implements Iterator { private Node current = head; public boolean hasNext() { return (current.next != head); } public E next() { if (!hasNext()) { throw new NoSuchElementException(); } current = current.next; return current.value; } } public Iterator iterator() { return new LinkedListIterator(); } public int size() { return size; } public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node p = head.next; for (int i = 0; i < index; i++) { p = p.next; } return p.value; } public void addFirst(E e) { if (e == null) { throw new NullPointerException(); } Node second = head.next; head.next = new Node(e, head, second); second.prev = head.next; size++; } }