forked from dale48/levawc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlist.c
322 lines (258 loc) · 6.71 KB
/
dlist.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: dlist.c
* Author : Kyle Loudon/Dan Levin
* Date : Fri Mar 22 12:40:45 GMT 2013
* Version : 0.40
* ---
* Description: A doubly-linked list - implemented as a pure, generic ADT.
*
* Revision history - coming up below:
*
* Date Revision message
* 2012-12-03 Created this file
* 2013-02-05 Created a new function 'int DLISTfind_remove(Dlist list, void **data)'
* 2013-02-19 Made some revision to the Doxygen documentation. Enhanced the description of
* in/out parameters - i.e. double-pointers.
*
*/
/**
* @file dlist.c
*
**/
#include "dlist.h"
struct DListElmt_
{
void *data;
struct DListElmt_ *prev;
struct DListElmt_ *next;
};
struct DList_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
struct DListElmt_ *head;
struct DListElmt_ *tail;
};
/* FUNCTION DEFINITIONS ------------------------------------------------------ */
Dlist DLISTinit(void (*destroy)(void *data))
{
Dlist list;
if ((list = (Dlist)malloc(sizeof(struct DList_))) == NULL)
return NULL;
list->size = 0;
list->match = NULL;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
return list;
}
void DLISTdestroy(Dlist list)
{
void *data;
while (DLISTsize(list) > 0)
{
if (DLISTremove(list, DLISTtail(list), (void **)&data) == 0 &&
list->destroy != NULL)
{
list->destroy(data);
}
}
free(list);
}
int DLISTinsnext(Dlist list, DlistNode node, const void *data)
{
DlistNode newnode;
/* If trying to insert a NULL node into a non-empty list.. */
if (node == NULL && DLISTsize(list) != 0)
return -1; /* --- Return error --- */
/* --- Allocate space for a new node --- */
if ((newnode = (DlistNode)malloc(sizeof(struct DListElmt_))) == NULL)
return -1;
newnode->data = (void *)data;
if (DLISTsize(list) == 0) /* --- List is empty --- */
{
/* --- Handle insertion of new node accordingly --- */
list->head = newnode;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = newnode;
}
else /* --- List is not empty --- */
{
newnode->next = node->next;
newnode->prev = node;
if (node->next == NULL) /* --- Node 'node' is tail.. --- */
list->tail = newnode;
else
node->next->prev = newnode;
node->next = newnode;
}
list->size++;
return 0;
}
int DLISTinsprev(Dlist list, DlistNode node, const void *data)
{
DlistNode newnode;
/* If trying to insert a NULL node into a non-empty list.. */
if (node == NULL && DLISTsize(list) != 0)
return -1; /* --- Return error --- */
/* --- Allocate space for a new node --- */
if ((newnode = (DlistNode)malloc(sizeof(struct DListElmt_))) == NULL)
return -1;
newnode->data = (void *)data;
if (DLISTsize(list) == 0) /* --- If list is empty --- */
{
/* --- Handle insertion of new node accordingly --- */
list->head = newnode;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = newnode;
}
else /* --- List is not empty --- */
{
newnode->next = node;
newnode->prev = node->prev;
if (node->prev == NULL) /* --- Node 'node' is head.. --- */
list->head = newnode;
else
node->prev->next = newnode;
node->prev = newnode;
}
list->size++;
return 0;
}
int DLISTremove(Dlist list, DlistNode node, void **data)
{
/* --- If trying to remove a NULL node or if list is empty --- */
if (node == NULL || DLISTsize(list) == 0)
return -1;
/* --- Remove and return data --- */
*data = node->data;
if (node == DLISThead(list)) /* --- If 'node' is head of list.. --- */
{
/* --- Handle removal of 'node' accordingly --- */
list->head = node->next;
if (list->head == NULL) /* --- 'list->head' now pointing at NULL --- */
list->tail = NULL;
else
node->next->prev = NULL;
}
else /* --- Handle removal when 'node' is inside list --- */
{
node->prev->next = node->next;
if (node->next == NULL) /* --- If 'node' is tail.. --- */
list->tail = node->prev;
else
node->next->prev = node->prev;
}
/* --- Free memory occupied by 'node' --- */
free(node);
/* --- Decrease number of nodes in the list --- */
list->size--;
return 0;
}
int DLISTsize(Dlist list)
{
return list->size;
}
DlistNode DLISThead(Dlist list)
{
return list->head;
}
DlistNode DLISTtail(Dlist list)
{
return list->tail;
}
int DLISTishead(Dlist list, DlistNode node)
{
return list->head == node ? 1 : 0;
}
int DLISTistail(Dlist list, DlistNode node)
{
return list->tail == node ? 1 : 0;
}
void *DLISTdata(DlistNode node)
{
return node->data;
}
DlistNode DLISTnext(DlistNode node)
{
return node->next;
}
DlistNode DLISTprev(DlistNode node)
{
return node->prev;
}
DlistNode DLISTfindnode(Dlist list, const void *data)
{
DlistNode member = NULL;
/* If match callback not set */
if (list->match == NULL)
return NULL;
for (member = list->head; member != NULL; member = member->next)
{
if (list->match(data, DLISTdata(member)))
break;
}
return member;
}
void DLISTsetmatch(Dlist list, int (*match)(const void *key1, const void *key2))
{
list->match = match;
}
int DLISTfind_remove(Dlist list, void **data)
{
DlistNode member;
/* If match-callback not set */
if (list->match == NULL)
return -2;
/* Search list sequentially.. */
member = DLISTfindnode(list, *data);
if (member == NULL) /* Node not found */
return 1;
/* Perform the removal.. */
return DLISTremove(list, member, data);
}
void DLISTsort(Dlist list, int (*cmp)(const void *key1, const void *key2))
{
DlistNode curr, maxmin, tmp;
void *tmpdata;
if (list->size > 1)
{
curr = list->head;
while (curr->next)
{
maxmin = curr;
tmp = curr->next;
while(tmp)
{
if (cmp(tmp->data, maxmin->data) < 0)
maxmin = tmp;
tmp = tmp->next;
}
if (maxmin != curr)
{
tmpdata = curr->data;
curr->data = maxmin->data;
maxmin->data = tmpdata;
}
curr = curr->next;
}
}
}
void DLISTtraverse(Dlist list, void (*callback)(const void *data), int direction)
{
DlistNode curr;
if (direction == DLIST_FWD)
for (curr=list->head; curr != NULL; curr=curr->next)
callback(curr->data);
if (direction == DLIST_BWD)
for (curr=list->tail; curr != NULL; curr=curr->prev)
callback(curr->data);
}