aboutsummaryrefslogtreecommitdiff
path: root/src/LinkedList.h
diff options
context:
space:
mode:
authorJakob Lykke Andersen <Jakob@caput.dk>2025-11-29 17:01:50 +0100
committerJakob Lykke Andersen <Jakob@caput.dk>2025-11-29 17:01:50 +0100
commit9d4394e359e69010114c1ddbef8a9e7fb53c5f9e (patch)
tree8492d6a53413b95a53399297916542d77fdb386b /src/LinkedList.h
parent8b0e8a51f032504cc9b506ed6e5c54e996164fd0 (diff)
downloadcycle-detector-9d4394e359e69010114c1ddbef8a9e7fb53c5f9e.tar.gz
cycle-detector-9d4394e359e69010114c1ddbef8a9e7fb53c5f9e.zip
Add/update/reset files
Diffstat (limited to 'src/LinkedList.h')
-rw-r--r--src/LinkedList.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/LinkedList.h b/src/LinkedList.h
new file mode 100644
index 0000000..f30d44b
--- /dev/null
+++ b/src/LinkedList.h
@@ -0,0 +1,50 @@
+#ifndef LINKEDLIST_H
+#define LINKEDLIST_H
+
+// A linked list containing any type of pointer.
+// The linked list does _not_ own its elements.
+
+typedef struct LinkedList LinkedList;
+typedef struct LinkedListNode LinkedListNode;
+
+struct LinkedList {
+ LinkedListNode *head;
+ LinkedListNode *tail;
+ int size;
+};
+
+struct LinkedListNode {
+ LinkedListNode *next;
+ LinkedListNode *prev;
+ void *data;
+};
+
+// 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();
+
+// Deallocate the given linked list, including all nodes
+// (but _not_ the data they point to, the user owns that).
+void LinkedList_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);
+
+// Remove and return the first element from the given list.
+// Pre: ll->size != 0
+void *LinkedList_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);
+
+// 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);
+
+#endif // LINKEDLIST_H