// linkedListFromFile.cc P. Conrad demo of linked list
// 4/26/06 for CISC181

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


struct Node_S
{
  int data;
  Node_S *next;
};


void  printOutTheList(Node_S *head);

int main( int argc, char  *argv []  )
{
  // check the command line arguments

  if (argc != 2)
    {
      cerr << "Usage: " << argv[0] << endl;
      exit(1);
    }

  // try to open the data file

  ifstream inf(argv[1],ios::in);

  if (!inf)
    {
      cerr << "Error: can't open " << argv[1]  << endl;
      exit(2);
    }

  // declare and initialize variables for a linked list

  Node_S *head; 
  Node_S *tail; 
  Node_S *p; 

  head = tail = NULL; // initialized an empty list

  // construct the linked list from the file

  int x;

  inf >> x; // try to read
  while (!inf.eof () )
    {

      // process the x that we read

      p = new Node_S;  // creates a new node
      p->data = x;
      p->next = NULL;

      if (head == NULL)
	head = p;
      else
	tail->next = p;

      tail = p;

      // try to read the next item

      inf >> x;
    }


  // echo back the list to the screen

  printOutTheList(head);

  return 0;

}

void  printOutTheList(Node_S *head)
{
  // 1337 code to print out a linked list

  for (Node_S *p = head; p; p=p->next)
    cout << p->data << endl;

}


