Source of LList.java


  1: /**
  2:    A linked implemention of the ADT list.
  3:    @author Frank M. Carrano
  4:    @author Timothy M. Henry
  5:    @version 5.0
  6: */
  7: public class LList<T> implements ListInterface<T>
  8: {
  9:         private Node firstNode;            // Reference to first node of chain
 10:         private int  numberOfEntries;
 11:    
 12:         public LList()
 13:         {
 14:                 initializeDataFields();
 15:         } // end default constructor
 16:         
 17:         public void clear()
 18:         {
 19:                 initializeDataFields();
 20:         } // end clear
 21:    
 22: /*  < Implementations of the public methods add, remove, replace, getEntry, contains,
 23:       getLength, isEmpty, and toArray go here. >
 24:    . . . */
 25:   
 26:    // Initializes the class's data fields to indicate an empty list.
 27:    private void initializeDataFields()
 28:    {
 29:                 firstNode = null;
 30:                 numberOfEntries = 0;
 31:    } // end initializeDataFields
 32:    
 33:    // Returns a reference to the node at a given position.
 34:    // Precondition: The chain is not empty;
 35:    //               1 <= givenPosition <= numberOfEntries.
 36:    private Node getNodeAt(int givenPosition)
 37:    {
 38:       // Assertion: (firstNode != null) &&
 39:       //            (1 <= givenPosition) && (givenPosition <= numberOfEntries)
 40:       Node currentNode = firstNode;
 41:       
 42:       // Traverse the chain to locate the desired node
 43:       // (skipped if givenPosition is 1)
 44:       for (int counter = 1; counter < givenPosition; counter++)
 45:          currentNode = currentNode.getNextNode();
 46:       // Assertion: currentNode != null
 47:       return currentNode;
 48:    } // end getNodeAt
 49:   
 50:         private class Node
 51:         {
 52:       private T    data; // Entry in list
 53:       private Node next; // Link to next node
 54:       
 55:       private Node(T dataPortion)
 56:       {
 57:          data = dataPortion;
 58:          next = null;
 59:       } // end constructor
 60:       
 61:       private Node(T dataPortion, Node nextNode)
 62:       {
 63:          data = dataPortion;
 64:          next = nextNode;
 65:       } // end constructor
 66:       
 67:       private T getData()
 68:       {
 69:          return data;
 70:       } // end getData
 71:       
 72:       private void setData(T newData)
 73:       {
 74:          data = newData;
 75:       } // end setData
 76:       
 77:       private Node getNextNode()
 78:       {
 79:          return next;
 80:       } // end getNextNode
 81:       
 82:       private void setNextNode(Node nextNode)
 83:       {
 84:          next = nextNode;
 85:       } // end setNextNode
 86:         } // end Node
 87: } // end LList