From c29bf2efbdc4f4186f3fe571601b4d1acac4b321 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Fri, 7 Jul 2023 20:08:13 -0400 Subject: Implement trivial linked list * Bring-your-own-node (generic / zero allocations) * Doubly-linked and circular, forward and backward traversable * Random insert/removal in constant time * All operations are no-fail * [Some type safety concessions though] Signed-off-by: Malfurious --- CMakeLists.txt | 1 + list.c | 32 ++++++++++++++++++++++++++++++++ list.h | 10 ++++++++++ 3 files changed, 43 insertions(+) create mode 100644 list.c create mode 100644 list.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a13a74..b8644a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_compile_definitions( add_executable(${PROJECT_NAME} console.c helpers.c + list.c misplays.c ) diff --git a/list.c b/list.c new file mode 100644 index 0000000..9cf1ad0 --- /dev/null +++ b/list.c @@ -0,0 +1,32 @@ +#include +#include "list.h" + +struct _list { + LINKEDLIST; +}; + +void list_init(struct list *list) { + list->tail = list; + list->head = list; + list->end = list; +} + +void list_insert(void *_next, void *_node) { + struct _list *node = _node; + struct _list *next = _next; + struct _list *prev = next->prev; + next->prev = node; + prev->next = node; + node->next = next; + node->prev = prev; +} + +void list_remove(void *_node) { + struct _list *node = _node; + struct _list *next = node->next; + struct _list *prev = node->prev; + next->prev = prev; + prev->next = next; + node->next = NULL; + node->prev = NULL; +} diff --git a/list.h b/list.h new file mode 100644 index 0000000..7d7a272 --- /dev/null +++ b/list.h @@ -0,0 +1,10 @@ +#pragma once +#define LINKEDLIST void *prev, *next + +struct list { + void *tail, *head, *end; +}; + +extern void list_init(struct list *list); +extern void list_insert(void *_next, void *_node); +extern void list_remove(void *_node); -- cgit v1.2.3