1  import java.util.NoSuchElementException;
  2  
  3  /**
  4     A linked list is a sequence of nodes with efficient
  5     element insertion and removal. This class 
  6     contains a subset of the methods of the standard
  7     java.util.LinkedList class.
  8  */
  9  public class LinkedList
 10  {  
 11     private Node first;
 12     
 13     /** 
 14        Constructs an empty linked list.
 15     */
 16     public LinkedList()
 17     {  
 18        first = null;
 19     }
 20     
 21     /**
 22        Returns the first element in the linked list.
 23        @return the first element in the linked list
 24     */
 25     public Object getFirst()
 26     {  
 27        if (first == null) 
 28           throw new NoSuchElementException();
 29        return first.data;
 30     }
 31  
 32     /**
 33        Removes the first element in the linked list.
 34        @return the removed element
 35     */
 36     public Object removeFirst()
 37     {  
 38        if (first == null) 
 39           throw new NoSuchElementException();
 40        Object element = first.data;
 41        first = first.next;
 42        return element;
 43     }
 44  
 45     /**
 46        Adds an element to the front of the linked list.
 47        @param element the element to add
 48     */
 49     public void addFirst(Object element)
 50     {  
 51        Node newNode = new Node();
 52        newNode.data = element;
 53        newNode.next = first;
 54        first = newNode;
 55     }
 56     
 57     /**
 58        Returns an iterator for iterating through this list.
 59        @return an iterator for iterating through this list
 60     */
 61     public ListIterator listIterator()
 62     {  
 63        return new LinkedListIterator();
 64     }
 65     
 66     class Node
 67     {  
 68        public Object data;
 69        public Node next;
 70     }
 71  
 72     class LinkedListIterator implements ListIterator
 73     {  
 74        private Node position;
 75        private Node previous;
 76        /**
 77           Constructs an iterator that points to the front
 78           of the linked list.
 79        */
 80        public LinkedListIterator()
 81        {  
 82           position = null;
 83           previous = null;
 84        }
 85        
 86        /**
 87           Moves the iterator past the next element.
 88           @return the traversed element
 89        */
 90        public Object next()
 91        {  
 92           if (!hasNext())
 93              throw new NoSuchElementException();
 94           previous = position; // Remember for remove
 95  
 96           if (position == null)
 97              position = first;
 98           else
 99              position = position.next;
100  
101           return position.data;
102        }
103        
104        /**
105           Tests if there is an element after the iterator position.
106           @return true if there is an element after the iterator position
107        */
108        public boolean hasNext()
109        {  
110           if (position == null)
111              return first != null;
112           else
113              return position.next != null;
114        }
115        
116        /**
117           Adds an element before the iterator position
118           and moves the iterator past the inserted element.
119           @param element the element to add
120        */
121        public void add(Object element)
122        {  
123           if (position == null)
124           {
125              addFirst(element);
126              position = first;
127           }
128           else
129           {  
130              Node newNode = new Node();
131              newNode.data = element;
132              newNode.next = position.next;
133              position.next = newNode;
134              position = newNode;
135           }
136           previous = position;
137        }
138        
139        /**
140           Removes the last traversed element. This method may
141           only be called after a call to the next() method.
142        */
143        public void remove()
144        {  
145           if (previous == position)
146              throw new IllegalStateException();
147  
148           if (position == first)
149           {
150              removeFirst();
151           }
152           else 
153           {  
154              previous.next = position.next;
155           }
156           position = previous;
157        }
158  
159        /**
160           Sets the last traversed element to a different value. 
161           @param element the element to set
162        */
163        public void set(Object element)
164        {
165           if (position == null)
166              throw new NoSuchElementException();
167           position.data = element;
168        }
169     }
170  }