39
39
* listSetFreeMethod.
40
40
*
41
41
* On error, NULL is returned. Otherwise the pointer to the new list. */
42
- list * listCreate (void )
43
- {
42
+ list * listCreate (void ) {
44
43
struct list * list ;
45
44
46
- if ((list = zmalloc (sizeof (* list ))) == NULL )
47
- return NULL ;
45
+ if ((list = zmalloc (sizeof (* list ))) == NULL ) return NULL ;
48
46
list -> head = list -> tail = NULL ;
49
47
list -> len = 0 ;
50
48
list -> dup = NULL ;
@@ -54,14 +52,13 @@ list *listCreate(void)
54
52
}
55
53
56
54
/* Remove all the elements from the list without destroying the list itself. */
57
- void listEmpty (list * list )
58
- {
55
+ void listEmpty (list * list ) {
59
56
unsigned long len ;
60
57
listNode * current , * next ;
61
58
62
59
current = list -> head ;
63
60
len = list -> len ;
64
- while (len -- ) {
61
+ while (len -- ) {
65
62
next = current -> next ;
66
63
if (list -> free ) list -> free (current -> value );
67
64
zfree (current );
@@ -74,10 +71,8 @@ void listEmpty(list *list)
74
71
/* Free the whole list.
75
72
*
76
73
* This function can't fail. */
77
- void listRelease (list * list )
78
- {
79
- if (!list )
80
- return ;
74
+ void listRelease (list * list ) {
75
+ if (!list ) return ;
81
76
listEmpty (list );
82
77
zfree (list );
83
78
}
@@ -88,12 +83,10 @@ void listRelease(list *list)
88
83
* On error, NULL is returned and no operation is performed (i.e. the
89
84
* list remains unaltered).
90
85
* On success the 'list' pointer you pass to the function is returned. */
91
- list * listAddNodeHead (list * list , void * value )
92
- {
86
+ list * listAddNodeHead (list * list , void * value ) {
93
87
listNode * node ;
94
88
95
- if ((node = zmalloc (sizeof (* node ))) == NULL )
96
- return NULL ;
89
+ if ((node = zmalloc (sizeof (* node ))) == NULL ) return NULL ;
97
90
node -> value = value ;
98
91
listLinkNodeHead (list , node );
99
92
return list ;
@@ -102,7 +95,7 @@ list *listAddNodeHead(list *list, void *value)
102
95
/*
103
96
* Add a node that has already been allocated to the head of list
104
97
*/
105
- void listLinkNodeHead (list * list , listNode * node ) {
98
+ void listLinkNodeHead (list * list , listNode * node ) {
106
99
if (list -> len == 0 ) {
107
100
list -> head = list -> tail = node ;
108
101
node -> prev = node -> next = NULL ;
@@ -121,12 +114,10 @@ void listLinkNodeHead(list* list, listNode *node) {
121
114
* On error, NULL is returned and no operation is performed (i.e. the
122
115
* list remains unaltered).
123
116
* On success the 'list' pointer you pass to the function is returned. */
124
- list * listAddNodeTail (list * list , void * value )
125
- {
117
+ list * listAddNodeTail (list * list , void * value ) {
126
118
listNode * node ;
127
119
128
- if ((node = zmalloc (sizeof (* node ))) == NULL )
129
- return NULL ;
120
+ if ((node = zmalloc (sizeof (* node ))) == NULL ) return NULL ;
130
121
node -> value = value ;
131
122
listLinkNodeTail (list , node );
132
123
return list ;
@@ -151,8 +142,7 @@ void listLinkNodeTail(list *list, listNode *node) {
151
142
list * listInsertNode (list * list , listNode * old_node , void * value , int after ) {
152
143
listNode * node ;
153
144
154
- if ((node = zmalloc (sizeof (* node ))) == NULL )
155
- return NULL ;
145
+ if ((node = zmalloc (sizeof (* node ))) == NULL ) return NULL ;
156
146
node -> value = value ;
157
147
if (after ) {
158
148
node -> prev = old_node ;
@@ -181,8 +171,7 @@ list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
181
171
* The node is freed. If free callback is provided the value is freed as well.
182
172
*
183
173
* This function can't fail. */
184
- void listDelNode (list * list , listNode * node )
185
- {
174
+ void listDelNode (list * list , listNode * node ) {
186
175
listUnlinkNode (list , node );
187
176
if (list -> free ) list -> free (node -> value );
188
177
zfree (node );
@@ -211,8 +200,7 @@ void listUnlinkNode(list *list, listNode *node) {
211
200
* call to listNext() will return the next element of the list.
212
201
*
213
202
* This function can't fail. */
214
- listIter * listGetIterator (list * list , int direction )
215
- {
203
+ listIter * listGetIterator (list * list , int direction ) {
216
204
listIter * iter ;
217
205
218
206
if ((iter = zmalloc (sizeof (* iter ))) == NULL ) return NULL ;
@@ -254,8 +242,7 @@ void listRewindTail(list *list, listIter *li) {
254
242
* }
255
243
*
256
244
* */
257
- listNode * listNext (listIter * iter )
258
- {
245
+ listNode * listNext (listIter * iter ) {
259
246
listNode * current = iter -> next ;
260
247
261
248
if (current != NULL ) {
@@ -275,19 +262,17 @@ listNode *listNext(listIter *iter)
275
262
* the original node is used as value of the copied node.
276
263
*
277
264
* The original list both on success or error is never modified. */
278
- list * listDup (list * orig )
279
- {
265
+ list * listDup (list * orig ) {
280
266
list * copy ;
281
267
listIter iter ;
282
268
listNode * node ;
283
269
284
- if ((copy = listCreate ()) == NULL )
285
- return NULL ;
270
+ if ((copy = listCreate ()) == NULL ) return NULL ;
286
271
copy -> dup = orig -> dup ;
287
272
copy -> free = orig -> free ;
288
273
copy -> match = orig -> match ;
289
274
listRewind (orig , & iter );
290
- while ((node = listNext (& iter )) != NULL ) {
275
+ while ((node = listNext (& iter )) != NULL ) {
291
276
void * value ;
292
277
293
278
if (copy -> dup ) {
@@ -299,7 +284,7 @@ list *listDup(list *orig)
299
284
} else {
300
285
value = node -> value ;
301
286
}
302
-
287
+
303
288
if (listAddNodeTail (copy , value ) == NULL ) {
304
289
/* Free value if dup succeed but listAddNodeTail failed. */
305
290
if (copy -> free ) copy -> free (value );
@@ -320,13 +305,12 @@ list *listDup(list *orig)
320
305
* On success the first matching node pointer is returned
321
306
* (search starts from head). If no matching node exists
322
307
* NULL is returned. */
323
- listNode * listSearchKey (list * list , void * key )
324
- {
308
+ listNode * listSearchKey (list * list , void * key ) {
325
309
listIter iter ;
326
310
listNode * node ;
327
311
328
312
listRewind (list , & iter );
329
- while ((node = listNext (& iter )) != NULL ) {
313
+ while ((node = listNext (& iter )) != NULL ) {
330
314
if (list -> match ) {
331
315
if (list -> match (node -> value , key )) {
332
316
return node ;
@@ -349,12 +333,12 @@ listNode *listIndex(list *list, long index) {
349
333
listNode * n ;
350
334
351
335
if (index < 0 ) {
352
- index = (- index )- 1 ;
336
+ index = (- index ) - 1 ;
353
337
n = list -> tail ;
354
- while (index -- && n ) n = n -> prev ;
338
+ while (index -- && n ) n = n -> prev ;
355
339
} else {
356
340
n = list -> head ;
357
- while (index -- && n ) n = n -> next ;
341
+ while (index -- && n ) n = n -> next ;
358
342
}
359
343
return n ;
360
344
}
0 commit comments