Source of 18.9.java


  1: // @author Frank M. Carrano, Timothy M. Henry
  2: // @version 5.0

  4: protected final Node getFirstNode()
  5: {
  6:    return firstNode;
  7: } // end getFirstNode

  9: /** Adds a node to the beginning of a chain. */
 10: protected final void addFirstNode(Node theNode)
 11: {
 12:    // Assertion: theNode != null
 13:    theNode.setNextNode(firstNode);
 14:    firstNode = theNode;
 15:    numberOfEntries++;
 16: } // end addFirstNode

 18: /** Adds a node to a chain after a given node. */
 19: protected final void addAfterNode(Node nodeBefore, Node theNode)

 21: /** Removes a chain’s first node. */
 22: protected final T removeFirstNode()

 24: /** Removes the node after a given one. */
 25: protected final T removeAfterNode(Node nodeBefore)

 27: public T remove(int givenPosition)
 28: {
 29:    T result = null;

 31:    if ((givenPosition >= 1) && (givenPosition <= getLength()))
 32:    {
 33:       // Assertion: The list is not empty
 34:       if (givenPosition == 1)         // Case 1: Remove first entry
 35:          result = removeFirstNode();
 36:       else                            // Case 2: givenPosition > 1
 37:       {
 38:          Node nodeBefore = getNodeAt(givenPosition - 1);
 39:          result = removeAfterNode(nodeBefore);
 40:       } // end if
 41:       return result;                  // Return removed entry
 42:    }
 43:    else
 44:       throw new IndexOutOfBoundsException("Illegal position given to remove operation.");
 45: } // end remove