42 #define NR_OF_SLOTS 11
45 #define MAIN_MENU_ROW "--- OPEN-ADDRESSED HASH TABLE DEMO ---\nMENU: 0=Exit 1=Add_Node 2=Rem_Node 3=Search 4=Print\nSelection "
48 void my_destroy(
void *data);
49 void print(
const void *data);
50 int my_cmp(
const void *key1,
const void *key2);
51 int my_match(
const void *k1,
const void *k2);
52 int my_hash1(
const void *key);
53 int my_hash2(
const void *key);
56 void ins_node(
OHtbl tbl);
57 void rem_node(
OHtbl tbl);
58 void search_node(
OHtbl tbl);
59 void print_table(
OHtbl tbl);
60 void final_status(
OHtbl tbl);
63 void create_nodes(
OHtbl tbl,
int nr_of_nodes);
68 void my_destroy(
void *data)
74 void print(
const void *data)
76 printf(
"\n%02d", *(
int *)data);
80 int my_cmp(
const void *key1,
const void *key2)
82 return (*(
int *)key1 - *(
int *)key2);
86 int my_match(
const void *k1,
const void *k2)
88 return *(
int *)k1 == *(
int *)k2;
92 int my_hash1(
const void *key)
94 return (*(
const int *)key % NR_OF_SLOTS);
98 int my_hash2(
const void *key)
100 return 1 + (*(
const int *)key % (NR_OF_SLOTS - 2));
104 void create_nodes(
OHtbl tbl,
int nr_of_nodes)
106 int i=0, *pi, retval, dupctr=0;
109 printf(
"--- INITIALIZING A OPEN-ADDRESSED HASHTABLE, %d NODES, RANDOM INTEGER DATA ---", NR_OF_ITEMS);
113 pi = (
int *)malloc(
sizeof(
int));
139 }
while (++i < nr_of_nodes);
141 printf(
"\n\nCurrent table status:");
143 printf(
"\n\n%d/%d successful insertions -- %d duplicate(s) rejected...",
OHTBLsize(tbl), nr_of_nodes, dupctr);
148 void ins_node(
OHtbl tbl)
150 int tmp, *pi, retval;
156 printf(
"--- INSERT NODE ---");
157 printf(
"\n\nCurrent table status(%d nodes): ",
OHTBLsize(tbl));
160 tmp =
read_int(
"\nEnter data for node to be inserted (-1=Quit): ", 0, 0);
165 pi = (
int *)malloc(
sizeof(
int));
174 sprintf(mess,
"Error: Duplicate - node %d already present..!", *pi);
178 else if (retval == -1)
192 sprintf(mess,
"Node %d will be inserted..", *(
int *)pi);
199 void rem_node(
OHtbl tbl)
201 int tmp, *pi, retval;
207 printf(
"--- REMOVE NODE ---");
208 printf(
"\n\nCurrent table status(%d nodes): ",
OHTBLsize(tbl));
211 tmp =
read_int(
"\nEnter (key)data for node to be removed (-1=Quit): ", 0, 0);
217 if ((retval =
OHTBLremove(tbl, (
void **)&pi)) != OK)
222 sprintf(mess,
"Error: Node %d not found..!", *(
int *)pi);
227 printf(
"\nFatal failure - bailing out...");
235 sprintf(mess,
"Node %d will be removed..!", *(
int *)pi);
244 void search_node(
OHtbl tbl)
246 int tmp, *pi, retval;
252 printf(
"--- SEARCH NODE ---\n");
255 tmp =
read_int(
"\nEnter (key)data for node to be found (-1=Quit): ", 0, 0);
262 if ((retval =
OHTBLlookup(tbl, (
void **)&pi)) != OK)
267 sprintf(mess,
"Node %d NOT found..!", *(
int *)pi);
272 printf(
"Fatal failure - bailing out...");
280 sprintf(mess,
"Node %d FOUND..!", *(
int *)pi);
287 void print_table(
OHtbl tbl)
293 void final_status(
OHtbl tbl)
297 printf(
"--- FINAL STATUS ---");
298 printf(
"\n\nFinal table contents(%d nodes): ",
OHTBLsize(tbl));
308 srand((
unsigned int)time(NULL));
310 if ((mytbl =
OHTBLinit(NR_OF_SLOTS, my_hash1, my_hash2, my_match, my_destroy)) == NULL)
312 printf(
"\nFatal error - bailing out...\n!");
318 create_nodes(mytbl, NR_OF_ITEMS);
323 menu_choice =
menu(MAIN_MENU_ROW, 0, 4);
338 printf(
"--- PRINT TABLE ---");
339 printf(
"\n\nFinal table contents(%d nodes): ",
OHTBLsize(mytbl));