1: // Filename: LINKINT2.CPP
2: // Purpose: Illustrate a sequence of linked nodes.
4: #include <iostream.h>
5: #include <iomanip.h>
6:
8: typedef int DataType;
10: struct Node
11: {
12: DataType data;
13: Node* link;
14: };
15: typedef Node* NodePointer;
18: int main()
19: {
20: NodePointer head, current, next;
21:
22: // Get a first node and read a value into it
23: head = new Node;
24: current = head;
25: cin >> current->data;
26:
27: // Add a new node and read a value into it
28: next = new Node;
29: current->link = next;
30: current = next;
31: cin >> current->data;
32:
33: // Add a new node and read a value into it
34: next = new Node;
35: current->link = next;
36: current = next;
37: cin >> current->data;
38:
39: // Note that the preceding two code segments are identical,
40: // and either code segment could serve as the body of a loop
41: // to build a sequence of linked nodes.
43: // Make sure the sequence is terminated properly
44: current->link = NULL;
46: // Output the data value in each node
47: cout << endl;
48: current = head;
49: while (current != NULL)
50: {
51: cout << setw(5) << current->data;
52: current = current->link;
53: }
55: cout << endl << endl;
57: return 0;
58: }