aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikkel Thestrup <mithe24@student.sdu.dk>2025-12-04 09:56:07 +0100
committerMikkel Thestrup <mithe24@student.sdu.dk>2025-12-04 09:56:07 +0100
commit9d70c988533f0daa48b0a12ca0d3e40f09bd0f98 (patch)
tree8ebed22d456b9a68ea0a30f3352dbce55e764a33
parent11832055cad8e14aff78cc497b48aaa85e4fc972 (diff)
downloadcycle-detector-9d70c988533f0daa48b0a12ca0d3e40f09bd0f98.tar.gz
cycle-detector-9d70c988533f0daa48b0a12ca0d3e40f09bd0f98.zip
A few bug fixes in graph.c
-rw-r--r--src/graph.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/graph.c b/src/graph.c
index f3e7517..cd2d796 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -18,8 +18,8 @@ Graph *graph_new(int n) {
return NULL;
}
- graph->num_edges = n;
- graph->num_vertices = 0;
+ graph->num_edges = 0;
+ graph->num_vertices = n;
for (int i = 0; i < n; i++) {
graph->vertices[i].id = i;
@@ -57,6 +57,9 @@ void graph_remove_edge(Graph *g, int i, int j) {
Graph *graph_read(const char *filename) {
FILE *file = fopen(filename, "r");
+ if (!file)
+ return NULL;
+
char line[1024];
fgets(line, 1024, file);
@@ -66,7 +69,7 @@ Graph *graph_read(const char *filename) {
Graph *g = graph_new(n);
char c = line[0];
- int i, j = 0;
+ int i = 0, j = 0;
while (fgets(line, 1024, file)) {
while (c != '\n') {
if (c == '1') {