aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMikkel Thestrup <mithe24@student.sdu.dk>2025-12-11 14:38:07 +0100
committerMikkel Thestrup <mithe24@student.sdu.dk>2025-12-11 14:38:07 +0100
commit5706a0d893e014acca2b58e747273893a5cf16f0 (patch)
tree33416611c66a6b2435581b190317d28fe880245c /src
parent22202ef5bb15c55d484cf50d0b39470dbb57cf26 (diff)
downloadcycle-detector-5706a0d893e014acca2b58e747273893a5cf16f0.tar.gz
cycle-detector-5706a0d893e014acca2b58e747273893a5cf16f0.zip
Use c99 style of initializing structs
Diffstat (limited to 'src')
-rw-r--r--src/graph.c19
-rw-r--r--src/linked_list.c16
-rw-r--r--src/vector.c9
3 files changed, 21 insertions, 23 deletions
diff --git a/src/graph.c b/src/graph.c
index 8c19a19..2697745 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -12,21 +12,22 @@ Graph *graph_new(int n) {
if (!graph)
return NULL;
- graph->vertices = (Vertex *)malloc(sizeof(Vertex) * n);
+ *graph = (Graph) {
+ .vertices = (Vertex *)malloc(sizeof(Vertex) * n),
+ .num_vertices = n,
+ .num_edges = 0,
+ };
if (!graph->vertices) {
free(graph);
return NULL;
}
- graph->num_edges = 0;
- graph->num_vertices = n;
-
for (int i = 0; i < n; i++) {
- graph->vertices[i].id = i;
- graph->vertices[i].out_neighbours = vector_new();
- graph->vertices[i].in_neighbours = vector_new();
-
- // if allocation for the linked lists failed
+ graph->vertices[i] = (Vertex) {
+ .id = i,
+ .in_neighbours = vector_new(),
+ .out_neighbours = vector_new(),
+ };
if (!graph->vertices[i].out_neighbours
|| !graph->vertices[i].in_neighbours) {
graph_delete(graph);
diff --git a/src/linked_list.c b/src/linked_list.c
index 2e290e9..6335c3d 100644
--- a/src/linked_list.c
+++ b/src/linked_list.c
@@ -2,14 +2,11 @@
#include <stdlib.h>
LinkedList *linked_list_new(void) {
- LinkedList *ll = (LinkedList *)malloc(sizeof(LinkedList));
+ /* Note that the members of the linked list
+ * is initialized to 0 when using calloc() */
+ LinkedList *ll = (LinkedList *)calloc(1, sizeof(LinkedList));
if (!ll)
return NULL;
-
- ll->head = NULL;
- ll->tail = NULL;
- ll->size = 0;
-
return ll;
}
@@ -27,19 +24,18 @@ void linked_list_delete(LinkedList *ll) {
}
LinkedListNode *linked_list_append(LinkedList *ll, void *elem) {
- LinkedListNode *new_node = (LinkedListNode *)malloc(sizeof(LinkedListNode));
+ LinkedListNode *new_node = calloc(1, sizeof(LinkedListNode));
if (!new_node)
return NULL;
-
new_node->data = elem;
- new_node->next = NULL;
+
if (!ll->head) {
new_node->prev = NULL;
ll->head = new_node;
ll->tail = new_node;
} else {
- ll->tail->next = new_node;
new_node->prev = ll->tail;
+ ll->tail->next = new_node;
ll->tail = new_node;
}
diff --git a/src/vector.c b/src/vector.c
index 9884957..3b8f681 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -7,15 +7,16 @@ Vector *vector_new(void) {
Vector *v = (Vector *)malloc(sizeof(Vector));
if (!v)
return NULL;
-
- v->data = (void **)malloc(VECTOR_INITIAL_CAPACITY * sizeof(void *));
+ *v = (Vector) {
+ .data = malloc(VECTOR_INITIAL_CAPACITY * sizeof(void *)),
+ .size = 0,
+ .capacity = VECTOR_INITIAL_CAPACITY,
+ };
if (!v->data) {
free(v);
return NULL;
}
- v->size = 0;
- v->capacity = VECTOR_INITIAL_CAPACITY;
return v;
}