diff options
author | Kenny Levinsen <kl@kl.wtf> | 2020-08-03 02:11:29 +0200 |
---|---|---|
committer | Kenny Levinsen <kl@kl.wtf> | 2020-08-03 02:13:47 +0200 |
commit | 4afe674e54a1df7a86d3094d664cc2d1928f3662 (patch) | |
tree | cc820d6d0a2a04d7468df0dc737ab8c0646615b9 /common/linked_list.c | |
parent | c35b2d2d8848957c1761fe107352fd989c3915f4 (diff) |
linked_list: Initial implementation
Diffstat (limited to 'common/linked_list.c')
-rw-r--r-- | common/linked_list.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/common/linked_list.c b/common/linked_list.c new file mode 100644 index 0000000..b853289 --- /dev/null +++ b/common/linked_list.c @@ -0,0 +1,33 @@ +#include <assert.h> +#include <stdbool.h> +#include <stddef.h> + +#include "linked_list.h" + +void linked_list_init(struct linked_list *list) { + list->next = list; + list->prev = list; +} + +void linked_list_insert(struct linked_list *list, struct linked_list *elem) { + assert(list->prev != NULL && list->next != NULL); + assert(elem->prev == NULL && elem->next == NULL); + + elem->prev = list; + elem->next = list->next; + list->next = elem; + elem->next->prev = elem; +} + +void linked_list_remove(struct linked_list *elem) { + assert(elem->prev != NULL && elem->next != NULL); + + elem->prev->next = elem->next; + elem->next->prev = elem->prev; + elem->next = NULL; + elem->prev = NULL; +} + +bool linked_list_empty(struct linked_list *list) { + return list->next == list; +} |