From: Michael D. Lowis Date: Sun, 28 Jun 2015 16:27:29 +0000 (-0400) Subject: Added initial implementation of doubly linked list X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0e3bee807978e6500ad15827e69a93d60f0c29fb;p=archive%2Fcarl.git Added initial implementation of doubly linked list --- diff --git a/source/data/list.c b/source/data/list.c new file mode 100644 index 0000000..4ef2583 --- /dev/null +++ b/source/data/list.c @@ -0,0 +1,92 @@ +#include + +void list_init(list_t* list) +{ + list->head = NULL; + list->tail = NULL; +} + +bool list_empty(list_t* list) +{ + return (list->head == NULL); +} + +size_t list_size(list_t* list) +{ + size_t sz = 0; + list_node_t* node = list->head; + while (node != NULL) { + sz++; + node = node->next; + } + return sz; +} + +list_node_t* list_front(list_t* list) +{ + return list->head; +} + +void list_push_front(list_t* list, list_node_t* node) +{ + node->prev = NULL; + node->next = list->head; + list->head = node; + if (list->tail == NULL) + list->tail = node; +} + +list_node_t* list_pop_front(list_t* list) +{ + list_node_t* node = list->head; + list->head = node->next; + if (list->head == NULL) + list->tail = NULL; + node->next = NULL; + return node; +} + +list_node_t* list_back(list_t* list) +{ + return list->tail; +} + +void list_push_back(list_t* list, list_node_t* node) +{ + node->next = NULL; + node->prev = list->tail; + list->tail = node; + if (list->head == NULL) + list->head = node; +} + +list_node_t* list_pop_back(list_t* list) +{ + list_node_t* node = list->tail; + list->tail = node->prev; + if (list->tail == NULL) + list->head = NULL; + node->prev = NULL; + return node; +} + +bool list_node_has_next(list_node_t* node) +{ + return (node->next != NULL); +} + +list_node_t* list_node_next(list_node_t* node) +{ + return node->next; +} + +bool list_node_has_prev(list_node_t* node) +{ + return (node->prev != NULL); +} + +list_node_t* list_node_prev(list_node_t* node) +{ + return node->prev; +} + diff --git a/source/data/list.h b/source/data/list.h index 5a4141d..b6b413f 100644 --- a/source/data/list.h +++ b/source/data/list.h @@ -4,6 +4,8 @@ #ifndef LIST_H #define LIST_H +#include + typedef struct list_node_t { struct list_node_t* next; struct list_node_t* prev; @@ -32,11 +34,11 @@ void list_push_back(list_t* list, list_node_t* node); list_node_t* list_pop_back(list_t* list); -bool list_node_hasnext(list_node_t* node); +bool list_node_has_next(list_node_t* node); list_node_t* list_node_next(list_node_t* node); -bool list_node_hasprev(list_node_t* node); +bool list_node_has_prev(list_node_t* node); list_node_t* list_node_prev(list_node_t* node);