import java.util.NoSuchElementException;
public class LinkedList<E> extends AbstractList<E> {
private static class Node<T> {
private T value;
private Node<T> next;
private Node( T value, Node<T> next ) {
this.value = value;
this.next = next;
}
}
private Node<E> first = null;
private class LinkedListIterator implements Iterator<E> {
private Node<E> current;
public boolean hasNext() {
return ( ( ( current == null ) && ( first != null ) ) ||
( ( current != null ) && ( current.next != null ) ) );
}
public E next() {
if ( current == null ) {
current = first;
} else {
current = current.next;
}
if ( current == null ) {
throw new NoSuchElementException();
}
return current.value;
}
}
public Iterator<E> Iterator() {
return new LinkedListIterator();
}
public int size() {
Node<E> p = first;
int count = 0;
while ( p != null ) {
p = p.next;
count++;
}
return count;
}
public boolean addFirst( E e) {
boolean added = false;
if ( e != null ) {
first = new Node<E>( e, first );
added = true;
}
return added;
}
}