summaryrefslogtreecommitdiffstats
path: root/list.c
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2023-07-07 20:08:13 -0400
committerMalfurious <m@lfurio.us>2023-07-08 11:05:07 -0400
commitc29bf2efbdc4f4186f3fe571601b4d1acac4b321 (patch)
treef5a23bda3a58794679ec4ed6eb1ba7a95249954e /list.c
parent415c553d96c4851350512cc943e10ec477427e02 (diff)
downloadmisplays-c29bf2efbdc4f4186f3fe571601b4d1acac4b321.tar.gz
misplays-c29bf2efbdc4f4186f3fe571601b4d1acac4b321.zip
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 <m@lfurio.us>
Diffstat (limited to 'list.c')
-rw-r--r--list.c32
1 files changed, 32 insertions, 0 deletions
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 <stddef.h>
+#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;
+}