Source of ListWithTraversal.java


  1: import java.util.Iterator;
  2: import java.util.NoSuchElementException;
  3: /**
  4:    A linked implementation of the ADT list that
  5:    includes iterator operations as ADT operations.
  6:    @author Frank M. Carrano
  7:    @author Timothy M. Henry
  8:    @version 5.0
  9: */
 10: public class ListWithTraversal<T> implements ListInterface<T>, Iterator<T>
 11: {
 12:         private Node firstNode;
 13:         private int  numberOfEntries;
 14:         private Node nextNode; // Node containing next entry in iteration
 15:         
 16:         public ListWithTraversal()
 17:         {
 18:                 initializeDataFields();
 19:         } // end default constructor
 20:         
 21: /* < Implementations of the remaining methods of the ADT list go here;
 22:      you can see them in Chapter 12, beginning at Segment 12.7.>
 23:      . . . */
 24:    
 25:    // Initializes the class's data fields to indicate an empty list.
 26:    private void initializeDataFields()
 27:    {
 28:                 firstNode = null;
 29:                 numberOfEntries = 0;
 30:                 nextNode = null;
 31:    } // end initializeDataFields
 32:    
 33: // Methods in the interface Iterator go here:
 34: // 13.11
 35:    public T next()
 36:    {
 37:       if (hasNext())
 38:       {
 39:          Node returnNode = nextNode;        // Get next node
 40:          nextNode = nextNode.getNextNode(); // Advance iterator
 41:          
 42:          return returnNode.getData();       // Return next entry in iteration
 43:       }
 44:       else
 45:          throw new NoSuchElementException("Illegal call to next(); " +
 46:                                           "iterator is after end of list.");
 47:    } // end next
 48:    
 49:    // 13.12
 50:    public boolean hasNext()
 51:    {
 52:       return nextNode != null;
 53:    } // end hasNext
 54:    
 55:    // 13.13
 56:    public void remove()
 57:    {
 58:       throw new UnsupportedOperationException("remove() is not " +
 59:                                               "supported by this iterator");
 60:    } // end remove

 62:         /** Sets the traversal to the beginning of the list. 
 63:             This method is NOT in the interface Iterator. */
 64:    public void resetTraversal()
 65:    {
 66:       nextNode = firstNode;
 67:    } // end resetTraversal
 68:    
 69:         private class Node
 70:         {
 71:       private T    data; // Entry in list
 72:       private Node next; // Link to next node
 73:       
 74:       private Node(T dataPortion)
 75:       {
 76:          data = dataPortion;
 77:          next = null;
 78:       } // end constructor
 79:       
 80:       private Node(T dataPortion, Node nextNode)
 81:       {
 82:          data = dataPortion;
 83:          next = nextNode;
 84:       } // end constructor
 85:       
 86:       private T getData()
 87:       {
 88:          return data;
 89:       } // end getData
 90:       
 91:       private void setData(T newData)
 92:       {
 93:          data = newData;
 94:       } // end setData
 95:       
 96:       private Node getNextNode()
 97:       {
 98:          return next;
 99:       } // end getNextNode
100:       
101:       private void setNextNode(Node nextNode)
102:       {
103:          next = nextNode;
104:       } // end setNextNode
105:         } // end Node
106: } // end ListWithTraversal