diff options
| author | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-12-01 11:00:37 +0100 |
|---|---|---|
| committer | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-12-04 18:43:28 +0100 |
| commit | 0fb2f9061c76bae37121dcf689c5b952c043f304 (patch) | |
| tree | 894afd9dc4de6979f5340e5e021206e312d3c096 | |
| parent | 99df63cc27d34b9eee3c41a4ab43dfc3362ec949 (diff) | |
| download | cycle-detector-0fb2f9061c76bae37121dcf689c5b952c043f304.tar.gz cycle-detector-0fb2f9061c76bae37121dcf689c5b952c043f304.zip | |
Changed graph to use vector instead of linked list
Diffstat (limited to '')
| -rw-r--r-- | src/graph.c | 26 | ||||
| -rw-r--r-- | src/graph.h | 10 |
2 files changed, 12 insertions, 24 deletions
diff --git a/src/graph.c b/src/graph.c index 0a15f40..44ce400 100644 --- a/src/graph.c +++ b/src/graph.c @@ -1,5 +1,5 @@ #include "graph.h" -#include "linked_list.h" +#include "vector.h" #include <assert.h> #include <stdio.h> #include <stdlib.h> @@ -23,8 +23,8 @@ Graph *graph_new(int n) { for (int i = 0; i < n; i++) { graph->vertices[i].id = i; - graph->vertices[i].out_neighbours = linked_list_new(); - graph->vertices[i].in_neighbours = linked_list_new(); + graph->vertices[i].out_neighbours = vector_new(); + graph->vertices[i].in_neighbours = vector_new(); // if allocation for the linked lists failed if (!graph->vertices[i].out_neighbours @@ -38,23 +38,11 @@ Graph *graph_new(int n) { } void graph_add_edge(Graph *g, int i, int j) { - linked_list_append(g->vertices[i].out_neighbours, &g->vertices[j]); - linked_list_append(g->vertices[j].in_neighbours, &g->vertices[i]); + vector_push(g->vertices[i].out_neighbours, &g->vertices[j]); + vector_push(g->vertices[j].in_neighbours, &g->vertices[i]); g->num_edges++; } -void graph_remove_edge(Graph *g, int i, int j) { - LinkedListNode *in_node = linked_list_find( - g->vertices[i].out_neighbours, - &g->vertices[j]); - LinkedListNode *out_node = linked_list_find( - g->vertices[j].in_neighbours, - &g->vertices[j]); - linked_list_remove(g->vertices[i].out_neighbours, in_node); - linked_list_remove(g->vertices[j].in_neighbours, out_node); - g->num_edges--; -} - Graph *graph_read(const char *filename) { FILE *file = fopen(filename, "r"); if (!file) @@ -110,8 +98,8 @@ void graph_print(Graph *g) { void graph_delete(Graph *g) { for (int i = 0; i < g->num_vertices; i++) { - linked_list_delete(g->vertices[i].in_neighbours); - linked_list_delete(g->vertices[i].out_neighbours); + vector_delete(g->vertices[i].in_neighbours); + vector_delete(g->vertices[i].out_neighbours); } free(g->vertices); free(g); diff --git a/src/graph.h b/src/graph.h index 6461cc7..ca9f32a 100644 --- a/src/graph.h +++ b/src/graph.h @@ -9,7 +9,7 @@ #ifndef GRAPH_H #define GRAPH_H -#include "linked_list.h" +#include "vector.h" /** * @struct Vertex @@ -41,19 +41,19 @@ struct Vertex { /** * @brief List of outgoing neighbors (successors). - * @details A LinkedList containing pointers to Vertex structures that + * @details A Vector containing pointers to Vertex structures that * this vertex points to (outgoing edges). Each element in the * list is of type (Vertex*). */ - LinkedList *out_neighbours; + Vector *out_neighbours; /** * @brief List of incoming neighbors (predecessors). - * @details A LinkedList containing pointers to Vertex structures that + * @details A Vector containing pointers to Vertex structures that * point to this vertex (incoming edges). Each element in the * list is of type (Vertex*). */ - LinkedList *in_neighbours; + Vector *in_neighbours; }; /** |