13 /* Function prototypes */
14 hnode_t
*htable_alloc(unsigned int size
);
15 void htable_free(hnode_t
*htable
, unsigned int size
);
16 void htable_insert(hnode_t
*htable
, char *str
, unsigned int pos
);
17 unsigned int htable_search(hnode_t
*htable
, unsigned int size
, char *str
);
18 void htable_print(hnode_t
*htable
, unsigned int size
);
19 unsigned int htable_mkhash(char *str
, unsigned int size
);
25 myhtable
= htable_alloc(HTABLE_SIZE
);
27 htable_insert(myhtable
, "stathis", htable_mkhash("stathis", HTABLE_SIZE
));
28 htable_insert(myhtable
, "kostas", htable_mkhash("kostas", HTABLE_SIZE
));
29 htable_insert(myhtable
, "petros", htable_mkhash("petros", HTABLE_SIZE
));
30 htable_insert(myhtable
, "maria", htable_mkhash("maria", HTABLE_SIZE
));
32 printf("%d\n", htable_search(myhtable
, HTABLE_SIZE
, "stathis"));
33 printf("%d\n", htable_search(myhtable
, HTABLE_SIZE
, "kostas"));
34 printf("%d\n", htable_search(myhtable
, HTABLE_SIZE
, "petros"));
35 printf("%d\n", htable_search(myhtable
, HTABLE_SIZE
, "maria"));
37 htable_print(myhtable
, HTABLE_SIZE
);
39 htable_free(myhtable
, HTABLE_SIZE
);
44 hnode_t
*htable_alloc(unsigned int size
)
49 /* Allocate memory for `size' hnode_t structures */
50 if ((pnode
= malloc(size
* sizeof *pnode
)) == NULL
) {
55 /* Initialize hnode_t fields */
56 for (i
= 0; i
< size
; i
++) {
64 void htable_free(hnode_t
*htable
, unsigned int size
)
69 for (i
= 0; i
< size
; i
++) {
70 pnode
= htable
[i
].next
;
71 /* Does this pnode have a chain ? If yes, free it */
72 while (pnode
!= NULL
) {
78 if (htable
[i
].str
!= NULL
)
85 void htable_insert(hnode_t
*htable
, char *str
, unsigned int pos
)
89 /* Is `pos' available in the hash table ? */
90 if (htable
[pos
].str
== NULL
) {
91 if ((htable
[pos
].str
= malloc(sizeof(strlen(str
)))) == NULL
)
93 strcpy(htable
[pos
].str
, str
);
96 /* Collision resolution */
97 for (pnode
= &htable
[pos
]; pnode
->next
!= NULL
; pnode
= pnode
->next
)
99 pnode
->next
= htable_alloc(1);
100 if ((pnode
->next
->str
= malloc(sizeof(strlen(str
)))) == NULL
)
102 strcpy(pnode
->next
->str
, str
);
107 unsigned int htable_search(hnode_t
*htable
, unsigned int size
, char *str
)
113 pos
= htable_mkhash(str
, size
);
115 /* Is the `str' in the hash table ? */
116 if (strcmp(htable
[pos
].str
, str
) == 0)
119 /* Search across the chain */
120 for (pnode
= htable
[pos
].next
; pnode
!= NULL
; pnode
= pnode
->next
)
121 if (strcmp(pnode
->str
, str
) == 0)
125 return -1; /* Not found */
128 void htable_print(hnode_t
*htable
, unsigned int size
)
133 for (i
= 0; i
< size
; i
++) {
135 if (pnode
->str
!= NULL
) {
136 printf("%s ", pnode
->str
);
137 /* Does this pnode have a chain ? If yes, print it */
138 while (pnode
->next
!= NULL
) {
139 printf("%s ", pnode
->next
->str
);
147 unsigned int htable_mkhash(char *str
, unsigned int size
)
150 unsigned int i
, hash
= 5381;
152 for (i
= 0; i
< strlen(str
); i
++)
153 hash
= ((hash
<< 5) + hash
) + str
[i
];
155 return (hash
& 0x7FFFFFFF) % size
;