confusion of hashtab and linked list
Code:
struct nlist *lookup(char *);
char *strdup(char *);
/* install: put (name, defn) in hashtab */
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 defn */
if ((np->defn = strdup(defn)) == NULL)
return NULL;
return np;
}
Please look at the line with comment of many stars.
In my understanding, np->next should point to the address of next structure in the linked list. Why here does it point back to the hashtab? I read this code again and again, but just can't understand it.
It's an example in K&R "The C Programming Language", 6.6, Table Lookup.
:confused: