interface Collection {
public void add(A x);
public Iterator iterator();
}
interface Iterator {
public A next();
public boolean hasNext();
}
class NoSuchElementException extends RuntimeException {}
class LinkedList implements Collection {
protected class Node {
A elt;
Node next = null;
Node(A elt){ this.elt = elt; }
}
protected Node head = null, tail = null;
public LinkedList(){}
public void add(A elt){
if(head==null){ head = new Node(elt); tail = head; }
else { tail.next = new Node(elt); tail = tail.next; }
}
public Iterator iterator(){
return new Iterator(){
protected Node prt = head;
public boolean hasNext(){ return ptr!=null; }
public A next(){
if(ptr!=null){
A elt=ptr.elt; ptr=ptr.next; return elt;
} else
throw new NoSuchElementException();
}
};
}
}