addition: destroy graph

This commit is contained in:
2026-05-06 20:20:45 -06:00
parent 8b6ec8ce2e
commit d75adf8510
2 changed files with 21 additions and 2 deletions

View File

@@ -10,7 +10,8 @@ typedef struct Edge Edge;
typedef enum {
GRAPH_OK,
GRAPH_BAD_ALLOC,
GRAPH_NOT_FOUND,
GRAPH_VERTEX_NOT_FOUND,
GRAPH_NULL_ARG,
} GraphErr ;
typedef enum {
@@ -19,7 +20,7 @@ typedef enum {
} GraphType;
Graph *graph_create(GraphType type);
GraphErr graph_destroy(Graph *graph);
GraphErr graph_destroy(Graph **graph);
GraphErr graph_add_vertex(Graph *graph);

View File

@@ -34,3 +34,21 @@ Graph *graph_create(GraphType type) {
return graph;
}
GraphErr graph_destroy(Graph **graph) {
if (graph == NULL || *graph) {
return GRAPH_NULL_ARG;
}
size_t vertex_count = arraylist_size((*graph)->vertices);
for (size_t i = 0; i < vertex_count; i++) {
Vertex vertex;
arraylist_get((*graph)->vertices, i, &vertex);
arraylist_destroy(&vertex.edges);
}
arraylist_destroy(&(*graph)->vertices);
return GRAPH_OK;
}