Page 175 - ARM 64 Bit Assembly Language
P. 175

162 Chapter 6


                75  /**********************************************************/
                76  /* wl_increment adds one to the count of the given word.  */
                77  /* If the word is not in the list, then it is added with  */
                78  /* a count of one.                                    */
                79  void wl_increment(wordlist *list, char *word)
                80  {
                81  wordlistnode *newword;
                82  wordlistnode *wlst = wln_lookup(list->head,word);
                83  if((wlst == NULL)||(strcmp(wlst->word, word) != 0))
                84    {
                85      list->nwords++;
                86      newword = (wordlistnode*)malloc(sizeof(wordlistnode));
                87      if(newword == NULL)
                88        {
                89          printf("Unable to allocate wordlistnode\n");
                90          exit(1);
                91        }
                92      newword->word = strdup(word);
                93      newword->count = 1;
                94      if(wlst == NULL)
                95        {
                96          newword->next = list->head;
                97          list->head = newword;
                98        }
                99      else
               100        {
               101          newword->next = wlst->next;
               102          wlst->next = newword;
               103        }
               104    }
               105  else
               106    wlst->count++;
               107  }
               108
               109  /**********************************************************/
               110  /* wl_print prints a table showing the number         */
               111  /* of occurrences for each word, followed by the word.  */
               112  void wl_print(wordlist *list)
               113  {
               114  wordlistnode *wlist = list->head;
               115  while(wlist != NULL) {
               116    printf("%10d %s\n",wlist->count,wlist->word);
               117    wlist=wlist->next;
               118  }
               119  printf("There are %d unique words in the document\n",
               120         list->nwords);
               121  }
               122
               123  /**********************************************************/
   170   171   172   173   174   175   176   177   178   179   180