45 #define MAIN_MENU_ROW "--- CHAINED HASH TABLE DEMO ---\nMENU: 0=Exit 1=Add_Node 2=Rem_Node 3=Search 4=Print\nSelection "
47 #define NR_OF_ITEMS 30
48 #define NR_OF_BUCKETS 11
52 void my_destroy(
void *data);
53 void print(
const void *data);
54 int my_cmp(
const void *key1,
const void *key2);
55 int my_match(
const void *k1,
const void *k2);
56 int my_hash(
const void *key);
59 void ins_nodes(
CHtbl tbl);
60 void rem_nodes(
CHtbl tbl);
61 void find_node(
CHtbl tbl);
62 void print_table(
CHtbl tbl);
63 void final_status(
CHtbl tbl);
66 void create_nodes(
CHtbl list,
int nr_of_nodes);
71 void my_destroy(
void *data)
77 void print(
const void *data)
79 printf(
" %02d", *(
int *)data);
83 int my_cmp(
const void *key1,
const void *key2)
85 return (*(
int *)key1 - *(
int *)key2);
89 int my_match(
const void *k1,
const void *k2)
91 return *(
int *)k1 == *(
int *)k2;
95 int my_hash(
const void *key)
97 return *(
int *)key%NR_OF_BUCKETS;
101 void create_nodes(
CHtbl tbl,
int nr_of_nodes)
103 int i=0, *pi, retval, dupctr=0;
106 printf(
"--- INITIALIZING A CHAINED HASHTABLE, %d NODES, RANDOM INTEGER DATA ---", NR_OF_ITEMS);
110 pi = (
int *)malloc(
sizeof(
int));
129 }
while (++i < nr_of_nodes);
131 printf(
"\n\nCreated hash table - %d/%d successful insertions -- %d duplicates rejected...",
CHTBLsize(tbl), nr_of_nodes, dupctr);
133 printf(
"\nCurrent table status(%d nodes): ",
CHTBLsize(tbl));
139 void ins_nodes(
CHtbl tbl)
141 int tmp, *pi, retval;
147 printf(
"--- ADD NODE TO HASH TABLE ---\n");
148 printf(
"\nCurrent table status(%d nodes): ",
CHTBLsize(tbl));
151 tmp =
read_int(
"\nEnter data for node to be inserted (-1=Quit): ", 0, 0);
156 pi = (
int *)malloc(
sizeof(
int));
165 sprintf(mess,
"Error: Node %d already present (bucket %d)..!", *pi, (*pi)%NR_OF_BUCKETS);
178 sprintf(mess,
"Node will be %d inserted - in bucket %d..", *(
int *)pi, (*pi)%NR_OF_BUCKETS);
185 void rem_nodes(
CHtbl tbl)
187 int tmp, *pi, retval;
193 printf(
"--- REMOVE NODE FROM HASH TABLE ---\n");
194 printf(
"\nCurrent table status(%d nodes): ",
CHTBLsize(tbl));
197 tmp =
read_int(
"\nEnter data for node to be removed (-1=Quit): ", 0, 0);
203 if ((retval =
CHTBLremove(tbl, (
void **)&pi)) != OK)
208 sprintf(mess,
"Error: Node %d not found..!", *(
int *)pi);
213 printf(
"\nFatal failure - bailing out...");
221 sprintf(mess,
"Node %d will be removed - from bucket %d..!", *(
int *)pi, (*pi)%NR_OF_BUCKETS);
230 void find_node(
CHtbl tbl)
232 int tmp, *pi, retval;
238 printf(
"--- SEARCH NODE IN HASH TABLE ---\n");
239 printf(
"\nCurrent table status(%d nodes): ",
CHTBLsize(tbl));
242 tmp =
read_int(
"\nEnter data for node to be found (-1=Quit): ", 0, 0);
249 if ((retval =
CHTBLlookup(tbl, (
void **)&pi)) != OK)
251 sprintf(mess,
"\nNode %d NOT FOUND!", *(
int *)pi);
256 sprintf(mess,
"\nNode %d FOUND - in bucket %d..", *(
int *)pi, (*pi)%NR_OF_BUCKETS);
263 void print_table(
CHtbl tbl)
267 printf(
"--- PRINT TABLE ---");
268 printf(
"\n\nCurrent table status(%d nodes): ",
CHTBLsize(tbl));
274 void final_status(
CHtbl tbl)
277 printf(
"--- FINAL STATUS ---\n");
279 printf(
"\nFinal table status(%d nodes): ",
CHTBLsize(tbl));
289 srand((
unsigned int)time(NULL));
291 if ((mytbl =
CHTBLinit(NR_OF_BUCKETS, my_hash, my_match, my_destroy)) == NULL)
293 printf(
"\nFatal error - bailing out...\n!");
299 create_nodes(mytbl, NR_OF_ITEMS);
304 menu_choice =
menu(MAIN_MENU_ROW, 0, 4);