1 |
/* |
2 |
* Copyright (C) 2002-2003 Erik Fears |
3 |
* |
4 |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License |
6 |
* as published by the Free Software Foundation; either version 2 |
7 |
* of the License, or (at your option) any later version. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
* GNU General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to |
16 |
* |
17 |
* The Free Software Foundation, Inc. |
18 |
* 59 Temple Place - Suite 330 |
19 |
* Boston, MA 02111-1307, USA. |
20 |
* |
21 |
* |
22 |
*/ |
23 |
|
24 |
#include <stdlib.h> |
25 |
|
26 |
#include "memory.h" |
27 |
#include "list.h" |
28 |
|
29 |
|
30 |
node_t *node_create(void *data) |
31 |
{ |
32 |
node_t *node = xcalloc(sizeof *node); |
33 |
node->next = NULL; |
34 |
node->prev = NULL; |
35 |
node->data = (void *) data; |
36 |
|
37 |
return node; |
38 |
} |
39 |
|
40 |
list_t *list_create() |
41 |
{ |
42 |
list_t *list = xcalloc(sizeof *list); |
43 |
|
44 |
list->head = NULL; |
45 |
list->tail = NULL; |
46 |
|
47 |
list->elements = 0; |
48 |
|
49 |
return list; |
50 |
} |
51 |
|
52 |
node_t *list_add(list_t *list, node_t *node) |
53 |
{ |
54 |
|
55 |
if(list == NULL || node == NULL) |
56 |
return NULL; |
57 |
|
58 |
if(list->tail == NULL) |
59 |
{ |
60 |
list->head = node; |
61 |
list->tail = node; |
62 |
|
63 |
node->next = NULL; |
64 |
node->prev = NULL; |
65 |
} |
66 |
else |
67 |
{ |
68 |
node->prev = list->tail; |
69 |
list->tail->next = node; |
70 |
list->tail = node; |
71 |
node->next = NULL; |
72 |
} |
73 |
|
74 |
list->elements++; |
75 |
return node; |
76 |
} |
77 |
|
78 |
node_t *list_remove(list_t *list, node_t *node) |
79 |
{ |
80 |
if(list == NULL || node == NULL) |
81 |
return NULL; |
82 |
|
83 |
if(node == list->head) |
84 |
{ |
85 |
list->head = node->next; |
86 |
|
87 |
if(node->next) |
88 |
node->next->prev = NULL; |
89 |
else |
90 |
list->tail = NULL; |
91 |
} |
92 |
else if(node == list->tail) |
93 |
{ |
94 |
list->tail = list->tail->prev; |
95 |
list->tail->next = NULL; |
96 |
} |
97 |
else |
98 |
{ |
99 |
node->prev->next = node->next; |
100 |
node->next->prev = node->prev; |
101 |
} |
102 |
|
103 |
list->elements--; |
104 |
return node; |
105 |
} |
106 |
|
107 |
void list_free(list_t *list) |
108 |
{ |
109 |
MyFree(list); |
110 |
} |
111 |
|
112 |
void node_free(node_t *node) |
113 |
{ |
114 |
MyFree(node); |
115 |
} |