1
#include "lcthw_List.h"
6
return calloc(1, sizeof(List));
9
void List_destroy(List *list)
11
LIST_FOREACH(list, first, next, cur) {
22
void List_clear(List *list)
24
LIST_FOREACH(list, first, next, cur) {
30
void List_clear_destroy(List *list)
37
void List_push(List *list, void *value)
39
ListNode *node = calloc(1, sizeof(ListNode));
44
if(list->last == NULL) {
48
list->last->next = node;
49
node->prev = list->last;
59
void *List_pop(List *list)
61
ListNode *node = list->last;
62
return node != NULL ? List_remove(list, node) : NULL;
65
void List_unshift(List *list, void *value)
67
ListNode *node = calloc(1, sizeof(ListNode));
72
if(list->first == NULL) {
76
node->next = list->first;
77
list->first->prev = node;
87
void *List_shift(List *list)
89
ListNode *node = list->first;
90
return node != NULL ? List_remove(list, node) : NULL;
93
void *List_remove(List *list, ListNode *node)
97
check(list->first && list->last, "List is empty.");
98
check(node, "node can't be NULL");
100
if(node == list->first && node == list->last) {
103
} else if(node == list->first) {
104
list->first = node->next;
105
check(list->first != NULL, "Invalid list, somehow got a first that is NULL.");
106
list->first->prev = NULL;
107
} else if (node == list->last) {
108
list->last = node->prev;
109
check(list->last != NULL, "Invalid list, somehow got a next that is NULL.");
110
list->last->next = NULL;
112
ListNode *after = node->next;
113
ListNode *before = node->prev;
114
after->prev = before;
115
before->next = after;
119
result = node->value;