< previous page page_1052 next page >

Page 1052
struct NodeType                // Complete declaration
{
    ComponentType component;
    NodePtr       link;
};
The advantage of using a forward declaration is that we can declare the type of link to be NodePtr just as we declare head, currPtr, and newNodePtr to be of type NodePtr.
Given the declarations above, the following code fragment creates a dynamic linked list with the values 12.8, 45.2, and 70.1 as the components in the list.
#include <stddef.h>    // For NULL
  .
  .
  .
head = new NodeType;
head->component = 12.8;
newNodePtr = new NodeType;
newNodePtr->component = 45.2;
head->link = newNodePtr;
currPtr = newNodePtr;
newNodePtr = new NodeType;
newNodePtr->component = 70.1;
currPtr->link = newNodePtr;
newNodePtr->link = NULL;
currPtr = newNodePtr;
Let's go through each of these statements, describing in words what is happening and showing the linked list as it appears after the execution of the statement.
head = new NodeType;
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
A dynamic variable of type NodeType is created. The pointer to this new node is stored into head. Variable head is the external pointer to the list we are building.
1052-01.gif

 
< previous page page_1052 next page >