< previous page page_1065 next page >

Page 1065
while (currPtr != NULL)

cout << currPtr->component << ' '
currPtr = currPtr->link;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
The loop body repeats because currPtr is not NULL.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
The number 58 is printed.
currPtr is now NULL.
1065-01.gif
while (currPtr != NULL)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
The loop body is not repeated because currPtr is NULL.

Inserting into a Linked List
A function for inserting a component into a linked list must have a parameter: the item to be inserted. The phrase inserting into a linked list could mean either inserting the component at the top of the list (as the first node) or inserting the component into its proper place according to some ordering (alphabetic or numeric). Let's examine these two situations separately.
Inserting a component at the top of a list is easy because we don't have to search the list to find where the item belongs.
InsertTop (In: item)
Set newNodePtr = new NodeType
Set component member of 
*newNodePtr = item
Set link member of 
*newNodePtr = head
Set head = newNodePtr

This algorithm is coded in the following function.

 
< previous page page_1065 next page >