From d75adf8510c1cc2bb1608c4f337e96c790b74fd6 Mon Sep 17 00:00:00 2001 From: laentropia Date: Wed, 6 May 2026 20:20:45 -0600 Subject: [PATCH] addition: destroy graph --- include/graph.h | 5 +++-- src/graph.c | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/graph.h b/include/graph.h index d242263..844049a 100644 --- a/include/graph.h +++ b/include/graph.h @@ -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); diff --git a/src/graph.c b/src/graph.c index c036189..6fa8f88 100644 --- a/src/graph.c +++ b/src/graph.c @@ -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; +}