Page 182 - ARM 64 Bit Assembly Language
P. 182

Abstract data types 169


                    8  /**********************************************************/
                    9  /* The wordlistnode type is a binary tree of words and  */
                   10  /* the number of times each word has occurred.        */
                   11  typedef struct wlist_node{
                   12   char *word;
                   13   int count;
                   14   struct wlist_node *left, *right;
                   15   int height;
                   16  }wordlistnode;
                   17
                   18  /*********************************************************/
                   19  /* wln_alloc allocates and initializes a wordlistnode  */
                   20  wordlistnode *wln_alloc(char *word)
                   21  {
                   22   wordlistnode *newword;
                   23   newword = (wordlistnode*)malloc(sizeof(wordlistnode));
                   24   if(newword == NULL)
                   25     {
                   26       printf("Unable to allocate wordlistnode\n");
                   27       exit(1);
                   28     }
                   29   newword->word = strdup(word);
                   30   newword->count = 1;
                   31   newword->left = NULL;
                   32   newword->right = NULL;
                   33   newword->height = 1;
                   34   return newword;
                   35  }
                   36
                   37  /*********************************************************/
                   38  /* wln_free frees the storage of a wordlistnode      */
                   39  void wln_free(wordlistnode *root)
                   40  {
                   41   if(root == NULL)
                   42     return;
                   43   free(root->word);
                   44   wln_free(root->left);
                   45   wln_free(root->right);
                   46   free(root);
                   47  }
                   48
                   49  /*********************************************************/
                   50  /* wln_lookup is used to search the tree of words. It  */
                   51  /* returns a pointer to the wordlistnode. If the word is */
                   52  /* not in the list, then it returns NULL.            */
                   53  wordlistnode* wln_lookup(wordlistnode* root, char *word)
                   54  {
                   55   int cmp;
                   56   if(root != NULL)
   177   178   179   180   181   182   183   184   185   186   187