1: //linked_nodes_of_int4.CPP 3: #include <iostream> 4: using namespace std; 6: typedef int DataType; 7: struct Node; 8: typedef Node* NodePointer; 9: struct Node 10: { 11: DataType data; 12: NodePointer link; 13: }; 15: void BuildNodeSequence 16: ( 17: NodePointer& head //inout 18: ); 20: void DisplaySequenceValues 21: ( 22: NodePointer head //in 23: ); 25: int main() 26: { 27: cout << "\nThis program builds a sequence of linked nodes of " 28: "integers from values entered\non a single line from the " 29: "keyboard. It then displays all of the values in that\n" 30: "sequence, in order, with one blank space between each " 31: "two values."; 32: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 34: NodePointer head; 35: cout << "\nEnter values on line below, then press Enter:\n"; 36: BuildNodeSequence(head); 37: cout << "\nAll values have now been read in and stored in the " 38: "sequence of linked nodes.\nThe values in that sequence will " 39: "now be displayed."; 40: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 42: DisplaySequenceValues(head); 43: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 44: } 46: void BuildNodeSequence 47: ( 48: NodePointer& head //inout 49: ) 50: { 51: head = new Node; //Get a first node 52: NodePointer current = head; //Make current point at the first node 53: cin >> current->data; //Read a value directly into the first node 54: while (cin.peek() != '\n') //While end of line not reached 55: { 56: int nextValue; //Holds additional input values 57: cin >> nextValue; //Get a data value 58: NodePointer next = new Node; //Get a new node 59: current->link = next; //Attach current node to new node 60: current = next; //Make current point at new node 61: current->data = nextValue; //Put data value into new node 62: } 63: cin.ignore(80, '\n'); //Clear input stream 64: current->link = nullptr; //Make sure sequence is terminated properly 65: } 67: void DisplaySequenceValues 68: ( 69: NodePointer head //in 70: ) 71: { 72: NodePointer current = head; //Make current point at head of sequence 73: while (current != nullptr) //While the end of sequence not reached 74: { 75: cout << current->data << " "; //Output value pointed at by current 76: current = current->link; //Move current pointer to next node 77: } 78: }