#include<iostream.h>
// needed for strlen, strcpy
#include<strings.h>

// LinkedList Example.C
// burton rosenberg
// presented in class April 2, 1997.


struct Node {
   int count ;  // didn't use this in class today
   char * word ;
   Node * next ;
} ;


Node * newList () {

   // create the dummy header
   Node * n ;
   n = new Node ;
   n->word = NULL ;
   n->count = 0 ;
   n->next = NULL ;

   // return it to the caller. The caller remembers this
   // value and uses it to reference the entire list.
   return n ;
}

void insertNode(  Node * l,  char * w )
{
// adds word w into list l. List l uses a dummy header.
// it is this procedure's responsibility to duplicate w.

   // Step 1: create new Node.
   Node * p ;
   p = new Node ;

   // Step 2: fill in new Node's data slots
   p->count = 1 ;
   p->word = new char[strlen(w)+1] ;
   strcpy(p->word, w) ;
   p->next = l->next ;

   // Step 3: splice new Node into list.
   l->next = p ;
}

void printList( Node * list )
{
   // skip over the dummy header
   list = list->next ;

   // LOOP INVARIANT: list points to next word to cout.
   while ( list!=NULL ) {
      cout << list->word << endl ;
      list = list->next ;
   }
}


int main( int argc, char * argv[] ) 
{
   Node * myList ;
   myList = newList () ;

   int i ;
   for ( i=0; i<argc; i++ )
   {
      insertNode( myList, argv[i] ) ;
   }

   printList( myList ) ;
}

