aboutsummaryrefslogtreecommitdiff
path: root/src/graph.c
diff options
context:
space:
mode:
authorMikkel Thestrup <mithe24@student.sdu.dk>2025-12-01 10:55:37 +0100
committerMikkel Thestrup <mithe24@student.sdu.dk>2025-12-01 10:55:37 +0100
commitf1021bf0aa79d880fad4073635d4561d67152a9d (patch)
tree1a91915614955d35f2a4cb14c2cc3c7a94464179 /src/graph.c
parentf4ea7131029351cebc0a7de2e46d459dcb18b777 (diff)
downloadcycle-detector-f1021bf0aa79d880fad4073635d4561d67152a9d.tar.gz
cycle-detector-f1021bf0aa79d880fad4073635d4561d67152a9d.zip
Added graph implementation
Diffstat (limited to '')
-rw-r--r--src/graph.c27
1 files changed, 26 insertions, 1 deletions
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 <assert.h>
+#include <stdio.h>
#include <stdlib.h>
// 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;
}