Source of 14.11.java


  1: public void add(int newPosition, T newEntry)
  2: {
  3:    if ((newPosition >= 1) && (newPosition <= numberOfEntries + 1))
  4:    {
  5:       Node newNode = new Node(newEntry);
  6:       if (newPosition == 1)                  // Case 1
  7:       {
  8:          newNode.setNextNode(firstNode);
  9:          firstNode = newNode;
 10:       }
 11:       else                                                                                 // Case 2: list is not empty
 12:       {                                      // and newPosition > 1
 13:          Node nodeBefore = getNodeAt(newPosition - 1);
 14:          Node nodeAfter = nodeBefore.getNextNode();
 15:          newNode.setNextNode(nodeAfter);
 16:          nodeBefore.setNextNode(newNode);
 17:       } // end if
 18:       numberOfEntries++;
 19:    }
 20:    else
 21:       throw new IndexOutOfBoundsException(
 22:                 "Illegal position given to add operation.");
 23: } // end add
 24: // Version 4.0