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);
49 /* Insertion and deletion */
51 list_node_t *list_insert_head(list_t *list, void *data) {
52 list_node_t *node = list_alloc_node();
56 node->next = list->head;
60 node->next->prev = node;
69 list_node_t *list_insert_tail(list_t *list, void *data) {
70 list_node_t *node = list_alloc_node();
74 node->prev = list->tail;
78 node->prev->next = node;
87 list_node_t *list_insert_after(list_t *list, list_node_t *after, void *data) {
88 list_node_t *node = list_alloc_node();
91 node->next = after->next;
96 node->next->prev = node;
105 list_node_t *list_insert_before(list_t *list, list_node_t *before, void *data) {
108 node = list_alloc_node();
112 node->prev = before->prev;
116 node->prev->next = node;
125 void list_unlink_node(list_t *list, list_node_t *node) {
127 node->prev->next = node->next;
129 list->head = node->next;
132 node->next->prev = node->prev;
134 list->tail = node->prev;
139 void list_delete_node(list_t *list, list_node_t *node) {
140 list_unlink_node(list, node);
141 list_free_node(list, node);
144 void list_delete_head(list_t *list) {
145 list_delete_node(list, list->head);
148 void list_delete_tail(list_t *list) {
149 list_delete_node(list, list->tail);
152 void list_delete(list_t *list, const void *data) {
153 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
154 if(node->data == data)
155 list_delete_node(list, node);
158 /* Head/tail lookup */
160 void *list_get_head(list_t *list) {
162 return list->head->data;
167 void *list_get_tail(list_t *list) {
169 return list->tail->data;
174 /* Fast list deletion */
176 void list_delete_list(list_t *list) {
177 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
178 list_free_node(list, node);
185 void list_foreach_node(list_t *list, list_action_node_t action) {
186 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
190 void list_foreach(list_t *list, list_action_t action) {
191 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)