Page 174 - ARM 64 Bit Assembly Language
P. 174

Abstract data types 161


                   26  { wordlist* tmp;
                   27   tmp = (wordlist*)malloc(sizeof(wordlist));
                   28   if(tmp == NULL)
                   29     {
                   30       printf("Unable to allocate wordlist\n");
                   31       exit(1);
                   32     }
                   33   tmp->nwords = 0;
                   34   tmp->head = NULL;
                   35   return tmp;
                   36  }
                   37
                   38  /**********************************************************/
                   39  /* wl_free frees all the storage used by a wordlist   */
                   40  void wl_free(wordlist* wl)
                   41  {
                   42   wordlistnode *tmpa, *tmpb;
                   43   tmpa = wl->head;
                   44   while(tmpa != NULL)
                   45     {
                   46       tmpb = tmpa;
                   47       tmpa = tmpa->next;
                   48       free(tmpb->word);
                   49       free(tmpb);
                   50     }
                   51   free(wl);
                   52  }
                   53
                   54  /*********************************************************/
                   55  /* wln_lookup is used internally to search the list of  */
                   56  /* words. It returns a pointer to the wordlistnode. If  */
                   57  /* the word is not in the list, then it returns a pointer*/
                   58  /* to the place where the word should be inserted.  If  */
                   59  /* the insertion point is the head of the list, then it  */
                   60  /* returns NULL.                                     */
                   61  wordlistnode* wln_lookup(wordlistnode* lst, char *word)
                   62  {
                   63   wordlistnode *prev = NULL;
                   64   while((lst != NULL)&&(strcmp(lst->word, word)<0))
                   65     {
                   66       prev = lst;
                   67       lst = lst->next;
                   68     }
                   69   if((lst != NULL)&&(strcmp(lst->word, word) == 0))
                   70     return lst;
                   71   else
                   72     return prev;
                   73  }
                   74
   169   170   171   172   173   174   175   176   177   178   179