// WordList.C
// (c) 1996 Burt Rosenberg
// Sample program using linked lists

// Compile using CC -c WordList.C in the same directory
// with WordList.h.

#include <iostream.h>
#include "WordList.h"

Node::Node( char s[], int i ) 
{
   count = i ;
   for ( int n=0; n<size; n++ ) {
      item[n] = s[n] ;
      if ( s[n]=='\0' ) break ;
   }
   item[size-1] = '\0' ;
}

WordList::WordList(void) {
   head = tail = found = NULL ;
}

void WordList::
print(void) {
   Node * n ;
   n = head ;
   while ( n!=NULL ) {
      cout << n->count << " " << n->item << endl ;
      n = n->next ;
   }
}

void WordList::
addWord( char word[] ) {
   Node * nn = new Node( word, 1 ) ;
   if ( head==NULL ) {
      head = tail = nn ;
   }
   else {
      tail->next = nn ;
      tail = nn ;
   }
}

int mystrcmp( char s[], char t[] ) {
   int i, flag ;
   i = 0 ;
   flag = 1 ;
   while ( s[i] == t[i] ) {
      if ( s[i] == '\0' ) {
          flag = 0 ;
          break ;
      }
      i++ ;
   }
   return flag ;
}

int WordList::
findWord( char word[] ) {
   Node * n = head ;
   while ( n!=NULL ) {
      if ( mystrcmp(n->item, word)==0 ) break ;
      n = n->next ;
   }
   found = n ;
   if ( found ) return 1 ;
   return 0 ;
}

int WordList::
incrCount(void) {
   if ( found==NULL ) return 0 ;
   else return ++(found->count) ;
}

