| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 
 | static struct nlist *hashtab[HASHSIZE];
---------------------------------
struct nlist *install(char *name, char *defn)
{
struct nlist *np;
unsigned hashval;
   if ((np = lookup(name)) == NULL)     /*  not found  */
   {
   np = (struct nlist*) malloc(sizeof(*np));
      if (np == NULL || (np->name = strdup(name)) == NULL)
                              return NULL;
    
      hashval = hash(name);
      np->next = hashtab[hashval];
      hashtab[hashval] = np;
   }else /*  already there  */
   free((void*) np->defn);   /*  free previous definiition  */
   if ((np->defn = strdup(defn)) == NULL)
                        return NULL;
return np;
}
---------------------------------
void printHtable(struct nlist *np)
{
   for (; np != NULL; np = np->next)
     printf ("print   %-15s %-35s\n",np->defn, np->name);
}
---------------   main  ------------------
char *zero[4][2] = {
   {"Zero, world.", "zero"},
   {"Il était une fois dans la prairie.", "un"},
   {"Il était deux fois dans la prairie.", "deux"},
   {"Il était trois fois dans la prairie.", "trois"}
};
for (i=0; i<4;   i++){
         hashtab[i] = install(zero[i][0], zero[i][1]);
         printf ("for   %-15s %-35s\n",hashtab[i]->defn, hashtab[i]->name);
}
printHtable(hashtab[0]);
----------  fin de main ----------------------- | 
Partager