Source of linked_nodes_of_int2.cpp


  1: //linked_nodes_of_int2.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 "
 13:         "using an axiliary \"next\" pointer.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

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

 25:     NodePointer head = new Node;

 27:     NodePointer current = head;
 28:     current->data = 25;

 30:     NodePointer next = new Node;
 31:     current->link = next;
 32:     current = next;
 33:     current->data = 30;

 35:     next = new Node;
 36:     current->link = next;
 37:     current = next;
 38:     current->data = 35;

 40:     current->link = nullptr;


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