From 806a018b5f523a7f38104268ff7b06c900dd7d32 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 5 Apr 2026 10:45:04 -0700 Subject: Add list_empty and list_push --- list/include/list.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/list/include/list.h b/list/include/list.h index aadcb88..92f30f7 100644 --- a/list/include/list.h +++ b/list/include/list.h @@ -34,6 +34,9 @@ static inline void* alloc(size_t size) { } \ list.head = nullptr; +/// Whether the list is empty. +#define list_empty(list) (!list.head) + /// Prepend a value to the list. #define list_add(list, value) \ { \ @@ -46,6 +49,23 @@ static inline void* alloc(size_t size) { list.head = node; \ } +/// Append a value to the list. +#define list_push(list, value) \ + { \ + __typeof__(list.head) node = alloc(sizeof(*list.head)); \ + node->val = value; \ + node->next = 0; \ + if (!list.head) { \ + list.head = node; \ + } else { \ + __typeof__(list.head) next = list.head; \ + while (next && next->next) { \ + next = next->next; \ + } \ + next->next = node; \ + } \ + } + /// Delete the first occurrence of the value from the list. #define list_remove(list, search_value) \ for (__typeof__(list.head) iter = list.head; iter; iter = iter->next) { \ -- cgit v1.2.3