-
Notifications
You must be signed in to change notification settings - Fork 113
/
8.table-lookup.c
98 lines (77 loc) · 1.64 KB
/
8.table-lookup.c
1
2
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct nlist {
struct nlist *next;
char *name;
char *defn;
};
#define HASHSIZE 101
static struct nlist *hashtab[HASHSIZE];
unsigned hash(char *s);
struct nlist *lookup(char *s);
struct nlist *install(char *name, char *defn);
struct nlist *undef(char *name);
int main()
{
install("name", "Trump");
struct nlist *n = lookup("name");
if (n != NULL)
printf("n->defn: %s\n", n->defn);
else
printf("n == NULL\n");
install("name", "Obama");
lookup("test");
n = lookup("name");
printf("n->defn: %s\n", n->defn);
n = undef("name");
printf("n->defn: %s\n", n->defn);
free(n->name);
free(n->defn);
free(n);
n = lookup("name");
printf("n == NULL: %d\n", n == NULL);
return 0;
}
unsigned hash(char *s)
{
unsigned hashval;
for (hashval = 0; *s != '\0'; ++s)
hashval = *s + 31 * hashval;
return hashval % HASHSIZE;
}
struct nlist *lookup(char *s)
{
struct nlist *np;
for (np = hashtab[hash(s)]; np != NULL; np = np->next) {
if (strcmp(s, np->name) == 0)
return np;
}
return NULL;
}
struct nlist *install(char *name, char *defn)
{
struct nlist *np;
unsigned hashval;
if ((np = lookup(name)) == NULL) {
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
free((void *) np->defn);
if ((np->defn = strdup(defn)) == NULL)
return NULL;
return np;
}
struct nlist *undef(char *name)
{
struct nlist *np;
if ((np = lookup(name)) == NULL)
return NULL;
unsigned hashval = hash(name);
hashtab[hashval] = NULL;
return np;
}