Java Reference
In-Depth Information
firstNode = newNode;
lastNode = newNode;
}
else if (newPosition == 1)
{
newNode.setNextNode(firstNode);
firstNode = newNode;
}
else if (newPosition == numberOfEntries + 1)
{
lastNode.setNextNode(newNode);
lastNode = newNode;
}
else
{
Node nodeBefore = getNodeAt(newPosition - 1);
Node nodeAfter = nodeBefore.getNextNode();
newNode.setNextNode(nodeAfter);
nodeBefore.setNextNode(newNode);
} // end if
numberOfEntries++;
}
else
isSuccessful = false ;
return isSuccessful;
} // end add
14.24
Removing an entry from a list. Removing an entry can affect the tail reference in two cases:
Case 1: If the list contains one entry and we remove it, an empty list results, and we must set
both the head and tail references to null .
Case 2: If the list contains several entries and we remove the last one, we must change the tail
reference so that it references the new last entry.
Figure 14-10 illustrates these two cases.
FIGURE 14-10
Removing the last node from a chain that has both head and tail references when the
chain contains (a) one node; (b) more than one node
Before
After
(a)
firstNode
lastNode
firstNode
lastNode
Before
After
(b)
firstNode
lastNode
firstNode
lastNode
 
Search WWH ::




Custom Search