diff options
Diffstat (limited to 'list')
| -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) { \ |
