1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/* This file is part of GNU cflow
Copyright (C) 1997, 2005, 2006, 2007, 2009 Sergey Poznyakoff
GNU cflow is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
GNU cflow is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with GNU cflow; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include <cflow.h>
static struct linked_list *
deref_linked_list (struct linked_list **plist)
{
if (!*plist) {
struct linked_list *list = xmalloc(sizeof(*list));
list->free_data = NULL;
list->head = list->tail = NULL;
*plist = list;
}
return *plist;
}
struct linked_list *
linked_list_create(linked_list_free_data_fp fun)
{
struct linked_list *list = xmalloc(sizeof(*list));
list->free_data = fun;
list->head = list->tail = NULL;
return list;
}
void
linked_list_append(struct linked_list **plist, void *data)
{
struct linked_list *list = deref_linked_list (plist);
struct linked_list_entry *entry = xmalloc(sizeof(*entry));
entry->list = list;
entry->data = data;
entry->next = NULL;
entry->prev = list->tail;
if (list->tail)
list->tail->next = entry;
else
list->head = entry;
list->tail = entry;
}
#if 0
void
linked_list_prepend(struct linked_list **plist, void *data)
{
struct linked_list *list = deref_linked_list (plist);
struct linked_list_entry *entry = xmalloc(sizeof(*entry));
entry->list = list;
entry->data = data;
if (list->head)
list->head->prev = entry;
entry->next = list->head;
entry->prev = NULL;
list->head = entry;
if (!list->tail)
list->tail = entry;
}
#endif
void
linked_list_destroy(struct linked_list **plist)
{
if (plist && *plist) {
struct linked_list *list = *plist;
struct linked_list_entry *p;
for (p = list->head; p; ) {
struct linked_list_entry *next = p->next;
if (list->free_data)
list->free_data(p->data);
free(p);
p = next;
}
free(list);
*plist = NULL;
}
}
void
linked_list_unlink(struct linked_list *list, struct linked_list_entry *ent)
{
struct linked_list_entry *p;
if (p = ent->prev)
p->next = ent->next;
else
list->head = ent->next;
if (p = ent->next)
p->prev = ent->prev;
else
list->tail = ent->prev;
if (list->free_data)
list->free_data(ent->data);
free(ent);
}
void
linked_list_iterate(struct linked_list **plist,
int (*itr) (void *, void *), void *data)
{
struct linked_list *list;
struct linked_list_entry *p;
if (!*plist)
return;
list = *plist;
for (p = linked_list_head(list); p; ) {
struct linked_list_entry *next = p->next;
if (itr(p->data, data))
linked_list_unlink(list, p);
p = next;
}
if (!list->head)
linked_list_destroy(&list);
*plist = list;
}
int
data_in_list(void *data, struct linked_list *list)
{
struct linked_list_entry *p;
for (p = linked_list_head(list); p; p = p->next)
if (p->data == data)
return 1;
return 0;
}
|