From 5a2665c21b511ef90967af053f6a1d504b78afff Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Sun, 26 Mar 2023 15:53:00 +0200 Subject: Stack, queue and graph data structures implemented in C using linked lists. --- src/graph.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/graph.c (limited to 'src/graph.c') diff --git a/src/graph.c b/src/graph.c new file mode 100644 index 0000000..f7c4b1d --- /dev/null +++ b/src/graph.c @@ -0,0 +1,27 @@ +#include +#include "graph.h" + +struct vertexList *vertexAlloc(size_t length) { + struct vertexList *vertices = malloc(sizeof(*vertices) * length); + for(size_t i = 0; i < length; i++) { + vertices[i].first = NULL; + vertices[i].last = NULL; + } + return vertices; +} + +void vertexAdd( + struct vertexList *list, size_t toWhich, size_t whichOne +) { + struct vertex *new = malloc(sizeof(*new)); + new->number = whichOne; + new->next = NULL; + if(list[toWhich].first == NULL) { + list[toWhich].first = new; + list[toWhich].last = new; + return; + } + list[toWhich].last->next = new; + list[toWhich].last = new; +} + -- cgit v1.3.1