35 #define ERR_VTX_OR_EDGE_MISSING -2
36 #define ERR_VTX_NOT_ISOL -3
39 #define NR_OF_VERTICES 7
40 #define NR_OF_EDGES 20
43 #define MAIN_MENU_ROW "--- GRAPH DEMO ---\nMENU: 0=Exit 1=Add_Vertex 2=Rem_Vertex 3=Add_Edge 4=Rem_Edge 5=Print\nSelection "
46 void my_destroy(
void *
data);
47 void print(
const void *
data);
48 int my_cmp(
const void *key1,
const void *key2);
49 int my_match(
const void *k1,
const void *k2);
52 void ins_vertex(
Graph gr);
53 void rem_vertex(
Graph gr);
54 void ins_edge(
Graph gr);
55 void rem_edge(
Graph gr);
56 void print_graph(
Graph gr);
57 void final_status(
Graph gr);
60 void create_rand_vertices(
Graph gr,
int nr_of_nodes);
65 void my_destroy(
void *
data)
71 void printvtx(
const void *
data)
73 printf(
"%02d", *(
int *)data);
77 void printedge(
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 void create_rand_vertices(
Graph gr,
int nr_of_nodes)
97 int i=0, *pi, retval, dupctr=0;
101 pi = (
int *)malloc(
sizeof(
int));
106 if (retval == ERR_DUPL)
113 if (retval == ERR_FATAL)
120 }
while (++i < nr_of_nodes);
123 printf(
"--- INITIALIZING A GRAPH, %d VERTICES, RANDOM INTEGER DATA ---", NR_OF_VERTICES);
124 printf(
"\n\nCurrent graph status:");
126 printf(
"\n\n%d/%d successful insertion(s) -- %d duplicates rejected...",
GRAPHvcount(gr), nr_of_nodes, dupctr);
131 void ins_vertex(
Graph gr)
133 int tmp, *pi, retval;
139 printf(
"--- ADD VERTEX ---");
143 tmp =
read_int(
"\nEnter data for vertex to be inserted (-1=Quit): ", 0, 0);
148 pi = (
int *)malloc(
sizeof(
int));
154 if (retval == ERR_DUPL)
156 sprintf(mess,
"\nError: Vertex %d duplicate - insertion failed..", *pi);
169 sprintf(mess,
"\nVertex %d will be inserted..", *(
int *)pi);
176 void rem_vertex(
Graph gr)
178 int tmp, *pi, retval;
184 printf(
"--- REMOVE VERTEX ---");
188 tmp =
read_int(
"\nEnter data for vertex to be removed (-1=Quit): ", 0, 0);
198 if (retval == ERR_VTX_OR_EDGE_MISSING)
200 sprintf(mess,
"\nError: Vertex %d not found - removal failed..", *(
int *)pi);
205 if (retval == ERR_VTX_NOT_ISOL)
207 sprintf(mess,
"\nError: Vertex %d not isolated - removal failed..", *(
int *)pi);
212 printf(
"\nError: Fatal failure - bailing out...");
220 sprintf(mess,
"\nVertex %d will be removed..!", *(
int *)pi);
229 void ins_edge(
Graph gr)
231 int tmp1, tmp2, *pi, retval;
237 printf(
"--- ADD EDGE ---");
241 tmp1 =
read_int(
"\nEnter data for first vertex of edge to be created (-1=Quit): ", 0, 0);
246 tmp2 =
read_int(
"Enter data for adjacent vertex of edge to be created (-1=Quit): ", 0, 0);
251 pi = (
int *)malloc(
sizeof(
int));
257 if (retval == ERR_DUPL)
259 sprintf(mess,
"\nError: Duplicate edge - between %d and %d..", tmp1, *(
int *)pi);
265 if (retval == ERR_VTX_OR_EDGE_MISSING)
267 sprintf(mess,
"\nError: Vertex not found - insertion failed..");
273 printf(
"\nError: Fatal failure - bailing out...");
281 sprintf(mess,
"\nEdge between vertex %d and %d will inserted..!", tmp1, *(
int *)pi);
288 void rem_edge(
Graph gr)
290 int tmp1, tmp2, *pi, retval;
296 printf(
"--- REMOVE EDGE ---");
300 tmp1 =
read_int(
"\nEnter data for first vertex of edge to be removed (-1=Quit): ", 0, 0);
305 tmp2 =
read_int(
"Enter data for adjacent vertex of edge to be removed (-1=Quit): ", 0, 0);
312 if ((retval =
GRAPHremedge(gr, &tmp1, (
void **)&pi)) != OK)
315 if (retval == ERR_VTX_OR_EDGE_MISSING)
317 sprintf(mess,
"\nError: Edge between %d and %d not found..", tmp1, *(
int *)pi);
322 printf(
"\nError: Fatal error - bailing out...");
329 sprintf(mess,
"\nEdge between %d and %d will be removed..!", tmp1, *(
int *)pi);
338 void print_graph(
Graph gr)
344 void final_status(
Graph gr)
347 printf(
"--- FINAL STATUS ---");
358 srand((
unsigned int)time(NULL));
360 if ((mygraph =
GRAPHinit(my_match, my_destroy)) == NULL)
362 printf(
"\nFatal error - bailing out...\n!");
367 create_rand_vertices(mygraph, NR_OF_VERTICES);
372 menu_choice =
menu(MAIN_MENU_ROW, 0, 5);
390 printf(
"--- PRINT GRAPH AND EDGES ---\n");
391 print_graph(mygraph);
395 final_status(mygraph);