1 |
/* |
2 |
* Copyright (c) 2002-2003 Erik Fears |
3 |
* Copyright (c) 2014-2021 ircd-hybrid development team |
4 |
* |
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. |
9 |
* |
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. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
18 |
* USA |
19 |
*/ |
20 |
|
21 |
/*! \file list.c |
22 |
* \brief Maintains doubly-linked lists. |
23 |
* \version $Id$ |
24 |
*/ |
25 |
|
26 |
#include <stdlib.h> |
27 |
#include <assert.h> |
28 |
|
29 |
#include "memory.h" |
30 |
#include "list.h" |
31 |
|
32 |
|
33 |
node_t * |
34 |
node_create(void) |
35 |
{ |
36 |
node_t *node = xcalloc(sizeof *node); |
37 |
return node; |
38 |
} |
39 |
|
40 |
void |
41 |
node_free(node_t *node) |
42 |
{ |
43 |
xfree(node); |
44 |
} |
45 |
|
46 |
node_t * |
47 |
list_add(void *data, node_t *node, list_t *list) |
48 |
{ |
49 |
node->data = data; |
50 |
node->prev = NULL; |
51 |
node->next = list->head; |
52 |
|
53 |
/* Assumption: If list->tail != NULL, list->head != NULL */ |
54 |
if (list->head) |
55 |
list->head->prev = node; |
56 |
else if (list->tail == NULL) |
57 |
list->tail = node; |
58 |
|
59 |
list->head = node; |
60 |
list->elements++; |
61 |
|
62 |
return node; |
63 |
} |
64 |
|
65 |
node_t * |
66 |
list_remove(node_t *node, list_t *list) |
67 |
{ |
68 |
/* Assumption: If node->next == NULL, then list->tail == node |
69 |
* and: If node->prev == NULL, then list->head == node |
70 |
*/ |
71 |
if (node->next) |
72 |
node->next->prev = node->prev; |
73 |
else |
74 |
{ |
75 |
assert(list->tail == node); |
76 |
list->tail = node->prev; |
77 |
} |
78 |
|
79 |
if (node->prev) |
80 |
node->prev->next = node->next; |
81 |
else |
82 |
{ |
83 |
assert(list->head == node); |
84 |
list->head = node->next; |
85 |
} |
86 |
|
87 |
/* Set this to NULL does matter */ |
88 |
node->next = NULL; |
89 |
node->prev = NULL; |
90 |
list->elements--; |
91 |
|
92 |
return node; |
93 |
} |