diff options
| author | 3gg <3gg@shellblade.net> | 2026-04-05 10:45:04 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2026-04-05 10:45:04 -0700 |
| commit | 806a018b5f523a7f38104268ff7b06c900dd7d32 (patch) | |
| tree | 9dafb2f8dae0cbfb3fcdb81f534aaffa3f026ee2 | |
| parent | 0b9e1c98eb4a0ba9eb9ed4b6d9d3ef9c5a3591ee (diff) | |
Add list_empty and list_push
| -rw-r--r-- | list/include/list.h | 20 |
1 files changed, 20 insertions, 0 deletions
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) { | |||
| 34 | } \ | 34 | } \ |
| 35 | list.head = nullptr; | 35 | list.head = nullptr; |
| 36 | 36 | ||
| 37 | /// Whether the list is empty. | ||
| 38 | #define list_empty(list) (!list.head) | ||
| 39 | |||
| 37 | /// Prepend a value to the list. | 40 | /// Prepend a value to the list. |
| 38 | #define list_add(list, value) \ | 41 | #define list_add(list, value) \ |
| 39 | { \ | 42 | { \ |
| @@ -46,6 +49,23 @@ static inline void* alloc(size_t size) { | |||
| 46 | list.head = node; \ | 49 | list.head = node; \ |
| 47 | } | 50 | } |
| 48 | 51 | ||
| 52 | /// Append a value to the list. | ||
| 53 | #define list_push(list, value) \ | ||
| 54 | { \ | ||
| 55 | __typeof__(list.head) node = alloc(sizeof(*list.head)); \ | ||
| 56 | node->val = value; \ | ||
| 57 | node->next = 0; \ | ||
| 58 | if (!list.head) { \ | ||
| 59 | list.head = node; \ | ||
| 60 | } else { \ | ||
| 61 | __typeof__(list.head) next = list.head; \ | ||
| 62 | while (next && next->next) { \ | ||
| 63 | next = next->next; \ | ||
| 64 | } \ | ||
| 65 | next->next = node; \ | ||
| 66 | } \ | ||
| 67 | } | ||
| 68 | |||
| 49 | /// Delete the first occurrence of the value from the list. | 69 | /// Delete the first occurrence of the value from the list. |
| 50 | #define list_remove(list, search_value) \ | 70 | #define list_remove(list, search_value) \ |
| 51 | for (__typeof__(list.head) iter = list.head; iter; iter = iter->next) { \ | 71 | for (__typeof__(list.head) iter = list.head; iter; iter = iter->next) { \ |
