//****************************************************************** // CreateList program // This program includes the code in the first part of Chapter 16 // to create a dynamic linked list of integers, PLUS it goes on // to print out the resulting list // Assumption: The user types in at least one number //****************************************************************** #include <iostream> #include <cstddef> // For NULL using namespace std; typedef int ComponentType; struct NodeType; // Forward declaration typedef NodeType* NodePtr; struct NodeType { ComponentType component; NodePtr link; }; int main() { NodePtr head; // External pointer to list NodePtr newNodePtr; // Pointer to newest node NodePtr currPtr; // Pointer to last node ComponentType inputVal; head = new NodeType; cout << "Enter an integer: "; cin >> head->component; currPtr = head; cout << "Enter an integer (or EOF to quit): "; cin >> inputVal; while (cin) { newNodePtr = new NodeType; // Create new node newNodePtr->component = inputVal; // Set its component value currPtr->link = newNodePtr; // Link node into list currPtr = newNodePtr; // Set currPtr to last node cout << "Enter an integer (or EOF to quit): "; cin >> inputVal; } currPtr->link = NULL; // Mark end of list // Print out the resulting linked list cout << endl << "Resulting list is:" << endl; NodePtr ptr = head; while (ptr != NULL) { cout << ptr->component << ' '; ptr = ptr->link; } cout << endl; return 0; }