Source of LinkedListWithIterator.java


  1: import java.util.Iterator;
  2: import java.util.NoSuchElementException;
  3: /**
  4:    A class that implements the ADT list by using a chain of nodes.
  5:    The list has an iterator. The class is similar to LList.
  6:    
  7:    @author Frank M. Carrano
  8:    @author Timothy M. Henry
  9:    @version 4.0
 10: */
 11: public class LinkedListWithIterator<T> implements ListWithIteratorInterface<T>
 12: {
 13:    private Node firstNode;
 14:    private int  numberOfEntries;;
 15:    public LinkedListWithIterator()
 16:    {
 17:       initializeDataFields();
 18:    } // end default constructor
 19: /*        < Implementations of the methods of the ADT list go here;
 20:      you can see them in Chapter 14, beginning at Segment 14.7 >
 21:    . . . */
 22:    
 23:    public Iterator<T> iterator()
 24:    {
 25:            return new IteratorForLinkedList();
 26:    } // end iterator
 27:         public Iterator<T> getIterator()
 28:         {
 29:            return iterator();
 30:         } // end getIterator
 31:    
 32:         private class IteratorForLinkedList implements Iterator<T>
 33:         {
 34:       private Node nextNode;
 35:                 private IteratorForLinkedList()
 36:                 {
 37:                         nextNode = firstNode;
 38:                 } // end default constructor
 39:                 
 40:       // 15.11
 41:                 public T next()
 42:                 {
 43:                         if (hasNext())
 44:                         {
 45:                                 Node returnNode = nextNode;        // Get next node
 46:                                 nextNode = nextNode.getNextNode(); // Advance iterator
 47:                                 
 48:                                 return returnNode.getData();       // Return next entry in iteration
 49:                         }
 50:                         else
 51:                                 throw new NoSuchElementException("Illegal call to next(); " +
 52:                                                        "iterator is after end of list.");
 53:                 } // end next
 54:       // 15.12
 55:                 public boolean hasNext()
 56:                 {
 57:                         return nextNode != null;
 58:                 } // end hasNext
 59:       
 60:       // 15.13
 61:                 public void remove()
 62:                 {
 63:                         throw new UnsupportedOperationException("remove() is not " +
 64:                                                            "supported by this iterator");
 65:                 } // end remove
 66:         } // end IteratorForLinkedList
 67:         
 68:         private class Node
 69:         {
 70:       private T    data; // Entry in list
 71:       private Node next; // Link to next node
 72:       
 73:       private Node(T dataPortion)
 74:       {
 75:          data = dataPortion;
 76:          next = null;
 77:       } // end constructor
 78:       
 79:       private Node(T dataPortion, Node nextNode)
 80:       {
 81:          data = dataPortion;
 82:          next = nextNode;
 83:       } // end constructor
 84:       
 85:       private T getData()
 86:       {
 87:          return data;
 88:       } // end getData
 89:       
 90:       private void setData(T newData)
 91:       {
 92:          data = newData;
 93:       } // end setData
 94:       
 95:       private Node getNextNode()
 96:       {
 97:          return next;
 98:       } // end getNextNode
 99:       
100:       private void setNextNode(Node nextNode)
101:       {
102:          next = nextNode;
103:       } // end setNextNode
104:         } // end Node
105: } // end LinkedListWithIterator