From a23cb6d6f011950b11789898c10e63f4473a5200 Mon Sep 17 00:00:00 2001 From: Mikkel Thestrup Date: Sun, 30 Nov 2025 14:24:52 +0100 Subject: Updated every file and function to follow style guide --- src/linked_list.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/linked_list.h (limited to 'src/linked_list.h') diff --git a/src/linked_list.h b/src/linked_list.h new file mode 100644 index 0000000..4e6dadf --- /dev/null +++ b/src/linked_list.h @@ -0,0 +1,50 @@ +#ifndef LINKED_LIST_H +#define LINKED_LIST_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 *linked_list_new(); + +// Deallocate the given linked list, including all nodes +// (but _not_ the data they point to, the user owns that). +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 *linked_list_append(LinkedList *ll, void *elem); + +// Remove and return the first element from the given list. +// Pre: ll->size != 0 +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 *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 *linked_list_remove(LinkedList *ll, LinkedListNode *node); + +#endif // LINKED_LIST_H -- cgit v1.2.3-70-g09d2