41 #define NR_OF_ITEMS 10
44 #define MAIN_MENU_ROW "--- SET DEMO ---\nMENU: 0=Exit 1=Set1_Add 2=Set2_Add 3=Set1_Rem 4=Set2_Rem 5=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);
54 void ins_node(
Set set);
55 void rem_node(
Set set);
56 void print_sets(
Set set1,
Set set2);
57 void print_union_diff_intersec(
Set s1,
Set s2);
58 void final_status(
Set set1,
Set set2);
61 void create_random_nodes(
Set set,
int nr_of_nodes);
66 void my_destroy(
void *data)
72 void print(
const void *data)
74 printf(
" %02d", *(
int *)data);
78 int my_cmp(
const void *key1,
const void *key2)
80 return (*(
int *)key1 - *(
int *)key2);
84 int my_match(
const void *k1,
const void *k2)
86 return *(
int *)k1 == *(
int *)k2;
90 void create_random_nodes(
Set set,
int nr_of_nodes)
92 int i=0, *pi, retval, dupctr=0;
96 pi = (
int *)malloc(
sizeof(
int));
118 }
while (i < nr_of_nodes);
122 void ins_node(
Set set)
124 int tmp, *pi, retval;
130 printf(
"--- ADD NODE TO SET ---\n");
131 printf(
"\nCurrent set status(%d nodes): ",
SETsize(
set));
134 tmp =
read_int(
"\nEnter nodedata for node to be inserted (-1=Quit): ", 0, 0);
139 pi = (
int *)malloc(
sizeof(
int));
148 sprintf(mess,
"Error: Duplicate - node %d already present..!", *pi);
156 printf(
"\nMatch-callback is missing... - bailing out!");
170 sprintf(mess,
"Node %d will be inserted..", *(
int *)pi);
177 void rem_node(
Set set)
179 int tmp, *pi, retval;
185 printf(
"--- REMOVE NODE FROM SET ---\n");
186 printf(
"\nCurrent set status(%d nodes): ",
SETsize(
set));
189 tmp =
read_int(
"\nEnter (key)data for node to be removed (-1=Quit): ", 0, 0);
197 if ((retval =
SETremove(
set, (
void **)&pi)) != OK)
201 sprintf(mess,
"Error: Node %d not found..!", *(
int *)pi);
207 printf(
"\nMatch-callback is missing... - bailing out!");
209 printf(
"\nFatal error... - bailing out!");
217 sprintf(mess,
"Node %d will be removed..!", *(
int *)pi);
227 void print_sets(
Set set1,
Set set2)
229 printf(
"\n\nSet 1 (%d nodes): ",
SETsize(set1));
232 printf(
"\nSet 2 (%d nodes): ",
SETsize(set2));
238 void print_union_diff_intersec(
Set s1,
Set s2)
240 Set set_union, set_diff1, set_diff2, set_intersec;
248 printf(
"\nSet1 UNION Set2(%02d nodes) : ",
SETsize(set_union));
250 printf(
"\nSet1 DIFF Set2(%02d nodes) : ",
SETsize(set_diff1));
252 printf(
"\nSet2 DIFF Set1(%02d nodes) : ",
SETsize(set_diff2));
254 printf(
"\nSet1 INTSEC Set2(%02d nodes): ",
SETsize(set_intersec));
266 void final_status(
Set s1,
Set s2)
270 printf(
"--- FINAL STATUS ---\n");
271 printf(
"\nFinal set contents:");
281 srand((
unsigned int)time(NULL));
283 if ((myset1 =
SETinit(my_match, my_destroy)) == NULL)
285 printf(
"\nFatal error... - bailing out!");
290 if ((myset2 =
SETinit(my_match, my_destroy)) == NULL)
292 printf(
"\nFatal error... - bailing out!");
298 printf(
"--- INITIALIZING 2 SETS, %d NODES, RANDOM INTEGER DATA ---", NR_OF_ITEMS);
301 create_random_nodes(myset1, NR_OF_ITEMS);
302 create_random_nodes(myset2, NR_OF_ITEMS);
304 printf(
"\n\nCurrent set status: ");
305 print_sets(myset1, myset2);
311 menu_choice =
menu(MAIN_MENU_ROW, 0, 5);
330 printf(
"--- PRINT Set1 & Set2 - AND THEIR CORRESPONDING UNION, DIFF AND INTERSECTION ---");
331 print_sets(myset1, myset2);
333 print_union_diff_intersec(myset1, myset2);
337 final_status(myset1, myset2);