aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/graph.c46
-rw-r--r--src/linked_list.c27
2 files changed, 61 insertions, 12 deletions
diff --git a/src/graph.c b/src/graph.c
index 4be2b44..cf45e2b 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -1,16 +1,56 @@
#include "graph.h"
+#include "linked_list.h"
#include <stdlib.h>
+// Need to declare 'graph_delete' for use in 'graph_new'
+void graph_delete(Graph *g);
+
Graph *graph_new(int n) {
- Graph *graph = malloc(sizeof(Graph));
- if (!graph) return NULL;
+ Graph *graph = (Graph *)malloc(sizeof(Graph));
+ if (!graph)
+ return NULL;
+
+ graph->vertices = (Vertex *)malloc(sizeof(Vertex) * n);
+ if (!graph->vertices) {
+ free(graph);
+ return NULL;
+ }
graph->numEdges = n;
graph->numVertices = 0;
- graph->vertices = malloc(sizeof(Vertex[n]));
+
+ for (int i = 0; i < n; i++) {
+ graph->vertices[i].id = i;
+ graph->vertices[i].outNeighbours = linked_list_new();
+ graph->vertices[i].inNeighbours = linked_list_new();
+
+ // if allocation for the linked lists failed
+ if (!graph->vertices[i].outNeighbours
+ || !graph->vertices[i].inNeighbours) {
+ graph_delete(graph);
+ return NULL;
+ }
+ }
return graph;
}
void graph_add_edge(Graph *g, int i, int j) {
+ linked_list_append(g->vertices[i].outNeighbours, &g->vertices[j]);
+ linked_list_append(g->vertices[j].inNeighbours, &g->vertices[i]);
+ g->numEdges++;
+}
+
+Graph *graph_read(const char *filename) {
+ Graph *g = graph_new(69);
+ return g;
+}
+
+void graph_delete(Graph *g) {
+ for (int i = 0; i < g->numVertices; i++) {
+ linked_list_delete(g->vertices[i].inNeighbours);
+ linked_list_delete(g->vertices[i].outNeighbours);
+ }
+ free(g->vertices);
+ free(g);
}
diff --git a/src/linked_list.c b/src/linked_list.c
index 0a6850d..2e290e9 100644
--- a/src/linked_list.c
+++ b/src/linked_list.c
@@ -3,7 +3,8 @@
LinkedList *linked_list_new(void) {
LinkedList *ll = (LinkedList *)malloc(sizeof(LinkedList));
- if (!ll) return NULL;
+ if (!ll)
+ return NULL;
ll->head = NULL;
ll->tail = NULL;
@@ -27,7 +28,8 @@ void linked_list_delete(LinkedList *ll) {
LinkedListNode *linked_list_append(LinkedList *ll, void *elem) {
LinkedListNode *new_node = (LinkedListNode *)malloc(sizeof(LinkedListNode));
- if (!new_node) return NULL;
+ if (!new_node)
+ return NULL;
new_node->data = elem;
new_node->next = NULL;
@@ -46,7 +48,8 @@ LinkedListNode *linked_list_append(LinkedList *ll, void *elem) {
}
void *linked_list_popfront(LinkedList *ll) {
- if (!ll->head) return NULL;
+ if (!ll->head)
+ return NULL;
void *elem = ll->head->data;
LinkedListNode *node = ll->head;
@@ -58,13 +61,15 @@ void *linked_list_popfront(LinkedList *ll) {
}
LinkedListNode *linked_list_find(LinkedList *ll, void *elem) {
- if (!ll->head) return NULL;
+ if (!ll->head)
+ return NULL;
LinkedListNode *node = ll->head;
while (node) {
/* !NOTE should use a given comparator function
* instead of '==' operator */
- if (node->data == elem) return node;
+ if (node->data == elem)
+ return node;
node = node->next;
}
@@ -74,11 +79,15 @@ LinkedListNode *linked_list_find(LinkedList *ll, void *elem) {
void *linked_list_remove(LinkedList *ll, LinkedListNode *node) {
void *elem = node->data;
- if (node->prev) node->prev->next = node->next;
- else ll->head = node->next;
+ if (node->prev)
+ node->prev->next = node->next;
+ else
+ ll->head = node->next;
- if (node->next) node->next->prev = node->prev;
- else ll->tail = node->prev;
+ if (node->next)
+ node->next->prev = node->prev;
+ else
+ ll->tail = node->prev;
free(node);
ll->size--;