Source of linked_nodes_of_int1.cpp


  1: //linked_nodes_of_int1.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 by simply assigning\na value to the data field of "
 10:         "each node. It then displays all of the values in\nthat "
 11:         "sequence, in order, with one blank space between each "
 12:         "two values. This\nversion creates and links new nodes without "
 13:         "using an axiliary \"next\" pointer.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     typedef int DataType;
 17:     struct Node
 18:     {
 19:         DataType data;
 20:         Node* link;
 21:     };

 23:     //Build a 3-node sequence without using a "next" pointer
 24:     Node* head = new Node;
 25:     Node* current = head;
 26:     
 27:     current->data = 66;
 28:     current->link = new Node;
 29:     current = current->link;

 31:     current->data = 77;
 32:     current->link = new Node;
 33:     current = current->link;

 35:     current->data = 88;
 36:     current->link = nullptr;

 38:     
 39:     //Visit all nodes and display their values
 40:     current = head;
 41:     while (current != nullptr)
 42:     {
 43:         cout << current->data << " ";
 44:         current = current->link;
 45:     }
 46:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 47: }