From f1021bf0aa79d880fad4073635d4561d67152a9d Mon Sep 17 00:00:00 2001 From: Mikkel Thestrup Date: Mon, 1 Dec 2025 10:55:37 +0100 Subject: Added graph implementation --- src/graph.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/graph.c b/src/graph.c index cf45e2b..5f4b5d6 100644 --- a/src/graph.c +++ b/src/graph.c @@ -1,5 +1,7 @@ #include "graph.h" #include "linked_list.h" +#include +#include #include // Need to declare 'graph_delete' for use in 'graph_new' @@ -42,7 +44,30 @@ void graph_add_edge(Graph *g, int i, int j) { } Graph *graph_read(const char *filename) { - Graph *g = graph_new(69); + FILE *file = fopen(filename, "r"); + char line[1024]; + fgets(line, 1024, file); + + int n = atoi(line); + if (!n) + return NULL; + Graph *g = graph_new(n); + + char c = line[0]; + int i, j = 0; + while (fgets(line, 1024, file)) { + while (c != '\n') { + if (c == '1') { + graph_add_edge(g, i, j); + j++; + } else if (c == '0') { + j++; + } + c++; + } + i++; + } + return g; } -- cgit v1.2.3-70-g09d2