diff options
| author | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-11-30 14:24:52 +0100 |
|---|---|---|
| committer | Mikkel Thestrup <mithe24@student.sdu.dk> | 2025-11-30 14:24:52 +0100 |
| commit | a23cb6d6f011950b11789898c10e63f4473a5200 (patch) | |
| tree | e6f0c3c85e240848a03909639ac227db05f7da2a /src | |
| parent | 06be9b97b3cc575c69d037d6ba09950c0a53d7f3 (diff) | |
| download | cycle-detector-a23cb6d6f011950b11789898c10e63f4473a5200.tar.gz cycle-detector-a23cb6d6f011950b11789898c10e63f4473a5200.zip | |
Updated every file and function to follow style guide
Diffstat (limited to '')
| -rw-r--r-- | src/cycle_detection.h (renamed from src/cycleDetection.h) | 10 | ||||
| -rw-r--r-- | src/graph.c | 16 | ||||
| -rw-r--r-- | src/graph.h (renamed from src/Graph.h) | 12 | ||||
| -rw-r--r-- | src/linked_list.c (renamed from src/LinkedList.c) | 14 | ||||
| -rw-r--r-- | src/linked_list.h (renamed from src/LinkedList.h) | 18 | ||||
| -rw-r--r-- | src/main.c | 8 |
6 files changed, 47 insertions, 31 deletions
diff --git a/src/cycleDetection.h b/src/cycle_detection.h index e1b62e0..df9607e 100644 --- a/src/cycleDetection.h +++ b/src/cycle_detection.h @@ -1,13 +1,13 @@ -#ifndef CYCLEDETECTION_H -#define CYCLEDETECTION_H +#ifndef CYCLE_DETECTION_H +#define CYCLE_DETECTION_H -#include "Graph.h" +#include "graph.h" // Runs Kahn's algorithm on the graph, and outputs 'CYCLE DETECTED!\n' // if a DAG cannot be created, or the vertices as a list fx. '4, 0, 1, 3, 2\n' // representing an ordering in the DAG. // The output is printed to stdout. // The input may be altered in the process. -void cycleDetection(Graph *g); +void cycle_detection(Graph *g); -#endif // CYCLEDETECTION_H +#endif // CYCLE_DETECTION_H diff --git a/src/graph.c b/src/graph.c new file mode 100644 index 0000000..4be2b44 --- /dev/null +++ b/src/graph.c @@ -0,0 +1,16 @@ +#include "graph.h" +#include <stdlib.h> + +Graph *graph_new(int n) { + Graph *graph = malloc(sizeof(Graph)); + if (!graph) return NULL; + + graph->numEdges = n; + graph->numVertices = 0; + graph->vertices = malloc(sizeof(Vertex[n])); + + return graph; +} + +void graph_add_edge(Graph *g, int i, int j) { +} diff --git a/src/Graph.h b/src/graph.h index 3e7cfaa..ca90bbe 100644 --- a/src/Graph.h +++ b/src/graph.h @@ -1,7 +1,7 @@ #ifndef GRAPH_H #define GRAPH_H -#include "LinkedList.h" +#include "linked_list.h" typedef struct Vertex Vertex; typedef struct Graph Graph; @@ -20,21 +20,21 @@ struct Graph { // Allocates and constructs a new graph with n vertices. // Returns a pointer to the new graph, or NULL on error. // Post: the caller owns the graph. -Graph *Graph_new(int n); +Graph *graph_new(int n); // Adds an edge from the i'th to the j'th vertex (0-indexed). -void Graph_addEdge(Graph *g, int i, int j); +void graph_add_edge(Graph *g, int i, int j); // Reads a graph from the given file and returns a newly // constructed Graph representing it. // Returns a pointer to the read graph, or NULL on error. // Post: the caller owns the graph. -Graph *Graph_read(const char *filename); +Graph *graph_read(const char *filename); // Deallocates the given graph and all its associated memory. -void Graph_delete(Graph *g); +void graph_delete(Graph *g); // Prints some useful information about the given graph. -void Graph_print(Graph *g); +void graph_print(Graph *g); #endif // GRAPH_H diff --git a/src/LinkedList.c b/src/linked_list.c index aac05d6..c655fe4 100644 --- a/src/LinkedList.c +++ b/src/linked_list.c @@ -1,7 +1,7 @@ -#include "LinkedList.h" +#include "linked_list.h" #include <stdlib.h> -LinkedList *linkedlist_new(void) { +LinkedList *linked_list_new(void) { LinkedList *ll = malloc(sizeof(LinkedList)); if (!ll) return NULL; @@ -12,7 +12,7 @@ LinkedList *linkedlist_new(void) { return ll; } -void linkedlist_delete(LinkedList *ll) { +void linked_list_delete(LinkedList *ll) { LinkedListNode *node = ll->head; LinkedListNode *next; @@ -25,7 +25,7 @@ void linkedlist_delete(LinkedList *ll) { free(ll); } -LinkedListNode *linkedlist_append(LinkedList *ll, void *elem) { +LinkedListNode *linked_list_append(LinkedList *ll, void *elem) { LinkedListNode *new_node = malloc(sizeof(LinkedListNode)); if (!new_node) return NULL; @@ -45,7 +45,7 @@ LinkedListNode *linkedlist_append(LinkedList *ll, void *elem) { return new_node; } -void *linkedlist_popfront(LinkedList *ll) { +void *linked_list_popfront(LinkedList *ll) { if (!ll->head) return NULL; void *elem = ll->head->data; @@ -57,7 +57,7 @@ void *linkedlist_popfront(LinkedList *ll) { return elem; } -LinkedListNode *linkedlist_find(LinkedList *ll, void *elem) { +LinkedListNode *linked_list_find(LinkedList *ll, void *elem) { if (!ll->head) return NULL; LinkedListNode *node = ll->head; @@ -71,7 +71,7 @@ LinkedListNode *linkedlist_find(LinkedList *ll, void *elem) { return NULL; // Couldn't find the element } -void *linkedlist_remove(LinkedList *ll, LinkedListNode *node) { +void *linked_list_remove(LinkedList *ll, LinkedListNode *node) { void *elem = node->data; if (node->prev) node->prev->next = node->next; diff --git a/src/LinkedList.h b/src/linked_list.h index 363fb21..4e6dadf 100644 --- a/src/LinkedList.h +++ b/src/linked_list.h @@ -1,5 +1,5 @@ -#ifndef LINKEDLIST_H -#define LINKEDLIST_H +#ifndef LINKED_LIST_H +#define LINKED_LIST_H // A linked list containing any type of pointer. // The linked list does _not_ own its elements. @@ -22,29 +22,29 @@ struct LinkedListNode { // Allocate and initialize an empty linked list. // Returns: a pointer to the new linked list, or NULL on error. // Post: the caller owns the linked list. -LinkedList *linkedlist_new(); +LinkedList *linked_list_new(); // Deallocate the given linked list, including all nodes // (but _not_ the data they point to, the user owns that). -void linkedlist_delete(LinkedList *ll); +void linked_list_delete(LinkedList *ll); // Append a the given element to the list. // The linked list does _not_ take ownership over the element // (only the linked list node). // Returns: a pointer to the node with the new element, or NULL on error. -LinkedListNode *linkedlist_append(LinkedList *ll, void *elem); +LinkedListNode *linked_list_append(LinkedList *ll, void *elem); // Remove and return the first element from the given list. // Pre: ll->size != 0 -void *linkedlist_popfront(LinkedList *ll); +void *linked_list_popfront(LinkedList *ll); // Find the linked list node containing the given element. // Returns: a pointer to the found node, or NULL if the element was not found. -LinkedListNode *linkedlist_find(LinkedList *ll, void *elem); +LinkedListNode *linked_list_find(LinkedList *ll, void *elem); // Remove the given node from the given linked list (and deallocate it). // Pre: node must belong to ll // Returns: node->data -void *linkedlist_remove(LinkedList *ll, LinkedListNode *node); +void *linked_list_remove(LinkedList *ll, LinkedListNode *node); -#endif // LINKEDLIST_H +#endif // LINKED_LIST_H @@ -1,4 +1,4 @@ -#include "cycleDetection.h" +#include "cycle_detection.h" #include <stdio.h> #include <stdlib.h> @@ -11,9 +11,9 @@ int main(int argc, char **argv) { return 1; } - Graph *g = Graph_read(argv[1]); + Graph *g = graph_read(argv[1]); if(!g) return 2; - cycleDetection(g); - Graph_delete(g); + cycle_detection(g); + graph_delete(g); return 0; } |