2 list.c -- functions to deal with double linked lists
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 /* (De)constructors */
27 list_t *list_alloc(list_action_t delete) {
28 list_t *list = xzalloc(sizeof(list_t));
29 list->delete = delete;
34 void list_free(list_t *list) {
38 list_node_t *list_alloc_node(void) {
39 return xzalloc(sizeof(list_node_t));
42 void list_free_node(list_t *list, list_node_t *node) {
43 if(node->data && list->delete) {
44 list->delete(node->data);
50 /* Insertion and deletion */
52 list_node_t *list_insert_head(list_t *list, void *data) {
53 list_node_t *node = list_alloc_node();
57 node->next = list->head;
61 node->next->prev = node;
71 list_node_t *list_insert_tail(list_t *list, void *data) {
72 list_node_t *node = list_alloc_node();
76 node->prev = list->tail;
80 node->prev->next = node;
90 list_node_t *list_insert_after(list_t *list, list_node_t *after, void *data) {
91 list_node_t *node = list_alloc_node();
94 node->next = after->next;
99 node->next->prev = node;
109 list_node_t *list_insert_before(list_t *list, list_node_t *before, void *data) {
112 node = list_alloc_node();
116 node->prev = before->prev;
120 node->prev->next = node;
130 void list_unlink_node(list_t *list, list_node_t *node) {
132 node->prev->next = node->next;
134 list->head = node->next;
138 node->next->prev = node->prev;
140 list->tail = node->prev;
146 void list_delete_node(list_t *list, list_node_t *node) {
147 list_unlink_node(list, node);
148 list_free_node(list, node);
151 void list_delete_head(list_t *list) {
152 list_delete_node(list, list->head);
155 void list_delete_tail(list_t *list) {
156 list_delete_node(list, list->tail);
159 void list_delete(list_t *list, const void *data) {
160 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
161 if(node->data == data) {
162 list_delete_node(list, node);
166 /* Head/tail lookup */
168 void *list_get_head(list_t *list) {
170 return list->head->data;
176 void *list_get_tail(list_t *list) {
178 return list->tail->data;
184 /* Fast list deletion */
186 void list_delete_list(list_t *list) {
187 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) {
188 list_free_node(list, node);
196 void list_foreach_node(list_t *list, list_action_node_t action) {
197 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) {
202 void list_foreach(list_t *list, list_action_t action) {
203 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)