// linkedListFromFile2.cc P. Conrad demo of linked list
// 05/03/06 for CISC181

// Improved our code by factoring out the 
// adding into the linked list... instead of
// writing that all out inside the loop, we just
// call our function

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


#include "linkedList.h"

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 () )
    {
      insertAtTail(x,&head,&tail);
      inf >> x;
    }

  // echo back the list to the screen

  cout << "before" << endl;
  printList(head);

  cout << "Please enter a number to add at the tail: ";
  cin >> x;

  insertAtTail(x, &head, &tail);

  cout << "after" << endl;
  printList(head);

  return 0;

}
