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