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