1 |
/* |
2 |
* Copyright (c) 2002-2003 Erik Fears |
3 |
* Copyright (c) 2014-2015 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 |
#include <stdlib.h> |
22 |
|
23 |
#include "memory.h" |
24 |
#include "list.h" |
25 |
|
26 |
|
27 |
node_t * |
28 |
node_create(void *data) |
29 |
{ |
30 |
node_t *node = xcalloc(sizeof *node); |
31 |
|
32 |
node->data = data; |
33 |
|
34 |
return node; |
35 |
} |
36 |
|
37 |
list_t * |
38 |
list_create(void) |
39 |
{ |
40 |
list_t *list = xcalloc(sizeof *list); |
41 |
return list; |
42 |
} |
43 |
|
44 |
node_t * |
45 |
list_add(list_t *list, node_t *node) |
46 |
{ |
47 |
if (list == NULL || node == NULL) |
48 |
return NULL; |
49 |
|
50 |
if (list->tail == NULL) |
51 |
{ |
52 |
list->head = node; |
53 |
list->tail = node; |
54 |
|
55 |
node->next = NULL; |
56 |
node->prev = NULL; |
57 |
} |
58 |
else |
59 |
{ |
60 |
node->prev = list->tail; |
61 |
list->tail->next = node; |
62 |
list->tail = node; |
63 |
node->next = NULL; |
64 |
} |
65 |
|
66 |
++list->elements; |
67 |
return node; |
68 |
} |
69 |
|
70 |
node_t * |
71 |
list_remove(list_t *list, node_t *node) |
72 |
{ |
73 |
if (list == NULL || node == NULL) |
74 |
return NULL; |
75 |
|
76 |
if (node == list->head) |
77 |
{ |
78 |
list->head = node->next; |
79 |
|
80 |
if (node->next) |
81 |
node->next->prev = NULL; |
82 |
else |
83 |
list->tail = NULL; |
84 |
} |
85 |
else if (node == list->tail) |
86 |
{ |
87 |
list->tail = list->tail->prev; |
88 |
list->tail->next = NULL; |
89 |
} |
90 |
else |
91 |
{ |
92 |
node->prev->next = node->next; |
93 |
node->next->prev = node->prev; |
94 |
} |
95 |
|
96 |
--list->elements; |
97 |
return node; |
98 |
} |
99 |
|
100 |
void |
101 |
list_free(list_t *list) |
102 |
{ |
103 |
xfree(list); |
104 |
} |
105 |
|
106 |
void |
107 |
node_free(node_t *node) |
108 |
{ |
109 |
xfree(node); |
110 |
} |