This is the code for inserting Vertex and connect edges for GRAPH (from a textbook). I was wondering why do we need two task pointers (task1, task2) for creating Vertex and deleting edges. For example, the following code creates two Vertex: A, B and establishes an edge between A and B. Thanks for any help.
/**********************************************/
int main(int argc, char **argv) {
Graph graph;
DfsVertex *task, task1, *task2;
List list;
ListElmt *element;
char data1[STRSIZ];
int i;
/*-----------------Initialize the graph-------------------------*/
graph_init(&graph, match_task, destroy_task);
/*----------- Insert Vertex A and B-----------------------*/
if ((task = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;
if ((task->data = malloc(STRSIZ)) == NULL)
return 1;
strcpy((char *)task->data, "A");
fprintf(stdout, "Inserting vertex %s\n", (char *)task->data);
if (graph_ins_vertex(&graph, task) != 0)
return 1;
if ((task = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;
if ((task->data = malloc(STRSIZ)) == NULL)
return 1;
strcpy((char *)task->data, "B");
fprintf(stdout, "Inserting vertex %s\n", (char *)task->data);
if (graph_ins_vertex(&graph, task) != 0)
return 1;
/*-------------Inserts the edge between A and B ------------------*/
task1.data = data1; <===what is the purpose of this line?
if ((task2 = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;
if ((task2->data = malloc(STRSIZ)) == NULL)
return 1;
strcpy((char *)task1.data, "A");
strcpy((char *)task2->data, "C");
fprintf(stdout, "Inserting edge %s to %s\n", (char *)task1.data, (char *)
task2->data);
if (graph_ins_edge(&graph, &task1, task2) != 0)
return 1;
/**********************************************/
int main(int argc, char **argv) {
Graph graph;
DfsVertex *task, task1, *task2;
List list;
ListElmt *element;
char data1[STRSIZ];
int i;
/*-----------------Initialize the graph-------------------------*/
graph_init(&graph, match_task, destroy_task);
/*----------- Insert Vertex A and B-----------------------*/
if ((task = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;
if ((task->data = malloc(STRSIZ)) == NULL)
return 1;
strcpy((char *)task->data, "A");
fprintf(stdout, "Inserting vertex %s\n", (char *)task->data);
if (graph_ins_vertex(&graph, task) != 0)
return 1;
if ((task = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;
if ((task->data = malloc(STRSIZ)) == NULL)
return 1;
strcpy((char *)task->data, "B");
fprintf(stdout, "Inserting vertex %s\n", (char *)task->data);
if (graph_ins_vertex(&graph, task) != 0)
return 1;
/*-------------Inserts the edge between A and B ------------------*/
task1.data = data1; <===what is the purpose of this line?
if ((task2 = (DfsVertex *)malloc(sizeof(DfsVertex))) == NULL)
return 1;
if ((task2->data = malloc(STRSIZ)) == NULL)
return 1;
strcpy((char *)task1.data, "A");
strcpy((char *)task2->data, "C");
fprintf(stdout, "Inserting edge %s to %s\n", (char *)task1.data, (char *)
task2->data);
if (graph_ins_edge(&graph, &task1, task2) != 0)
return 1;
