// lab08.cpp

#include <iostream>
#include <cstddef>
using namespace std;

// linked-list node structure
struct NodeType {
  int       info;
  NodeType *next;
};

int main()
{
  /* head points to head of list and tail points to tail of list */
  NodeType *head = NULL;
  NodeType *tail = NULL;
  NodeType *p;    // use this pointer to allocate new nodes
  int x;          // use this variable to read input values

  cout << "Enter an integer: ";
  cin >> x;

  // create the 1st node and store the value of x in it
  p = new NodeType;
  p->info = x;
  
  // let head and tail point to the new node
  head = tail =p;
  
  cout << "Enter an integer: ";
  cin >> x;

  // create the 2nd node and store the value of x in it

  p = new NodeType;
  p->info = x;
  
  // add the 2nd node to the list
  tail->next = p;
  tail = p; 
  // create and add 3 more nodes to the list 
  
  for(int i = 0; i < 3; ++i) {

    cout << "Enter an integer: ";
    cin >> x;

    p = new NodeType;
    p->info = x;
    tail->next = p;
    tail = p;
  }
  // store NULL in the next field of the last node, which should be pointed
  // to by the pointer tail

 p->next = NULL;
 tail->next = NULL; 
 // using the pointer p, traverse the list and print the values stored in it 
 p = head; 

// for(int i = 0; i <= 5; i++)
while(p != NULL)
 {
    cout << p->info << endl;
    p = p->next;
  }

  return 0;
}

