Page 172 - ARM 64 Bit Assembly Language
P. 172

Abstract data types  159


                   31  { int MaxWordLength = 1024;
                   32   char *nextword, *cleanword;
                   33   wordlist *list;
                   34   nextword = (char*)malloc(MaxWordLength*sizeof(char));
                   35   list = wl_alloc();
                   36   while(scanf("%s",nextword) == 1)
                   37     {
                   38       cleanword = remove_punctuation(nextword);
                   39       if(strlen(cleanword)>0)
                   40         wl_increment(list,cleanword);
                   41       free(cleanword);
                   42     }
                   43   printf("Alphabetical List\n");
                   44   wl_print(list);
                   45   printf("\nNumerical List\n");
                   46   wl_print_numerical(list);
                   47   wl_free(list);
                   48   return 0;
                   49  }


                     The interface for the ADT is shown in Listing 6.5. There are several ways that the ADT could
                     be implemented. Note that the interface given in the header file does not show the internal
                     fields of the word list data type. Thus, any file which includes this header are allowed to de-
                     clare pointers to wordlist data types, but cannot access or modify any internal fields. The
                     list of words could be stored in an array, a linked list, a binary tree, or some other data struc-
                     ture. Also, the subroutines could be implemented in C or in some other language, including
                     assembly. Listing 6.6 shows an implementation in C using a linked list. Note that the function
                     for printing the word frequency list in numerical order has not been implemented. It will be
                     written in assembly language. Since the program is split into multiple files, it is a good idea
                     to use the make utility to build the executable program. A basic makefile is shown in List-
                     ing 6.7.

                                           Listing 6.5 C header for the wordlist ADT.

                    1  #ifndef LIST_H
                    2  #define LIST_H
                    3
                    4  /**********************************************************/
                    5  /* Define an opaque type, named wordlist              */
                    6  typedef struct wlist wordlist;
                    7
                    8  /**********************************************************/
                    9  /* wl_alloc allocates and initializes a new word list.  */
                   10  wordlist* wl_alloc();
                   11
                   12  /**********************************************************/
   167   168   169   170   171   172   173   174   175   176   177