diff options
Diffstat (limited to 'src/graph.c')
| -rw-r--r-- | src/graph.c | 19 |
1 files changed, 10 insertions, 9 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); |