|
|
|
|
|
|
|
IF head is not NULL
RevPrint rest of nodes in list
Print current node in list |
|
|
|
|
|
|
|
|
|
This algorithm can be coded directly as the following function: |
|
|
|
|
|
|
|
|
void RevPrint( /* in */ NodePtr head )
// Precondition:
// head points to a list node (or == NULL)
// Postcondition:
// IF head != NULL
// All nodes following *head have been output,
// then *head has been output
// ELSE
// No action has taken place
{
if (head != NULL)
{
RevPrint(head->link); // Recursive call
cout << head->component << endl;
}
// Empty else-clause is the base case
} |
|
|
|
|
|
|
|
|
This algorithm seems complex enough to warrant a code walk-through. We use the following list: |
|
|
|
|
|
|
|
|
Call 1: head points to the node containing 45 and is not NULL. Execution of this call pauses until the recursive call with the actual parameter head->link has been completed. |
|
|
|
|
|
|
|
|
Call 2: head points to the node containing 78 and is not NULL. Execution of this call pauses until the recursive call with the actual parameter head->link has been completed. |
|
|
|
|
|