Mastering Algorithms With C - Kyle Loudon [121]
if (element == NULL)
return -1;
/*****************************************************************************
* *
* Insert the second vertex into the adjacency list of the first vertex. *
* *
*****************************************************************************/
if ((retval = set_insert(&((AdjList *)list_data(element))->adjacent, data2))
!= 0) {
return retval;
}
/*****************************************************************************
* *
* Adjust the edge count to account for the inserted edge. *
* *
*****************************************************************************/
graph->ecount++;
return 0;
}
/*****************************************************************************
* *
* --------------------------- graph_rem_vertex --------------------------- *
* *
*****************************************************************************/
int graph_rem_vertex(Graph *graph, void **data) {
ListElmt *element,
*temp,
*prev;
AdjList *adjlist;
int found;
/*****************************************************************************
* *
* Traverse each adjacency list and the vertices it contains. *
* *
*****************************************************************************/
prev = NULL;
found = 0;
for (element = list_head(&graph->adjlists); element != NULL; element =
list_next(element)) {
/**************************************************************************
* *
* Do not allow removal of the vertex if it is in an adjacency list. *
* *
**************************************************************************/
if (set_is_member(&((AdjList *)list_data(element))->adjacent, *data))
return -1;
/**************************************************************************
* *
* Keep a pointer to the vertex to be removed. *
* *
**************************************************************************/
if (graph->match(*data, ((AdjList *)list_data(element))->vertex)) {
temp = element;
found = 1;
}
/**************************************************************************
* *
* Keep a pointer to the vertex before the vertex to be removed. *
* *
**************************************************************************/
if (!found)
prev = element;
}
/*****************************************************************************
* *
* Return if the vertex was not found. *
* *
*****************************************************************************/
if (!found)
return -1;
/*****************************************************************************
* *
* Do not allow removal of the vertex if its adjacency list is not empty. *
* *
*****************************************************************************/
if (set_size(&((AdjList *)list_data(temp))->adjacent) > 0)
return -1;
/*****************************************************************************
* *
* Remove the vertex. *
* *
*****************************************************************************/
if (list_rem_next(&graph->adjlists, prev, (void **)&adjlist) != 0)
return -1;
/*****************************************************************************
* *
* Free the storage allocated by the abstract datatype. *
* *
*****************************************************************************/
*data = adjlist->vertex;
free(adjlist);
/*****************************************************************************
* *
* Adjust the vertex count to account for the removed vertex. *
* *
*****************************************************************************/
graph->vcount--;
return 0;
}
/*****************************************************************************
* *
* ---------------------------- graph_rem_edge ---------------------------- *
* *
*****************************************************************************/
int graph_rem_edge(Graph *graph, void *data1, void **data2) {
ListElmt *element;
/*****************************************************************************