-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadlist.c
338 lines (295 loc) · 6.14 KB
/
adlist.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <stdlib.h>
#include "adlist.h"
list *listCreate(void)
/*//{{{*/
{
struct list *list;
if((list = malloc(sizeof(*list))) == NULL)
return NULL;
list->head = list->tail = NULL;
list->len = 0;
list->dup = list->free = list->match = NULL;
return list;
}
/*}}}*/
void listRelease(list *list)
/*//{{{*/
{
unsigned long len;
listNode *current, *next;
current = list->head;
len = list->len;
while(len --)
{
next = current->next;
if(list->free) list->free(current->value);
free(current);
current = next;
}
free(list);
}
/*}}}*/
list *listAddNodeHead(list *list, void *value)
/*{{{*/
{
listNode *node;
if((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if(list->len == 0)
{
list->head = list->tail = node;
node->prev = node->next = NULL;
}
else
{
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->len ++;
return list;
}
/*}}}*/
list *listAddNodeTail(list *list, void *value)
/*{{{*/
{
listNode *node;
node->value = value;
if(list->len == 0)
{
node->prev = node->next = NULL;
list->head = list->tail = node;
}
else
{
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len ++;
return list;
}
/*}}}*/
list *listInsertNode(list *list, listNode *old_node, void *value, int after)
/*{{{*/
{
listNode *node;
if((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if(after)
{
//在old_node后面
node->prev = old_node;
node->next = old_node->next;
if(list->tail == old_node)
{
list->tail = node;
}
}
else
{
node->prev = old_node->prev;
node->next = old_node;
if(list->head == old_node)
{
list->head = node;
}
}
if(node->prev != NULL)
{
node->prev->next = node;
}
if(node->next != NULL)
{
node->next->prev = node;
}
list->len ++;
return list;
}
/*}}}*/
list *listDelNode(list *list, listNode *node)
/*{{{*/
{
//既然要针对node->prev结构操作,一定要事先检查node->prev是不是null
if(node->prev)
node->prev->next = node->next;
else //node->prev为空怎么办?
list->head = node->next;
if(node->next)
node->next->prev = node->prev;
else
list->tail = node->prev;
if(list->free)
list->free(node->value);
free(node);
list->len --;
return list;
}
/*}}}*/
listIter *listGetIterator(list *list, int direction)
/*{{{*/
{
listIter *iter;
if((iter = malloc(sizeof(*iter))) == NULL)
return NULL;
if(direction == AL_START_HEAD)
iter->next = list->head;
else
iter->next = list->tail;
iter->direction = direction;
return iter;
}
/*}}}*/
void listReleaseIterator(listIter *iter)
/*{{{*/
{
free(iter);
}
/*}}}*/
void listRewind(list *list, listIter *iter)
/*{{{*/
{
iter->next = list->head;
iter->direction = AL_START_HEAD;
}
/*}}}*/
void listRewindTail(list *list, listIter *iter)
/*{{{*/
{
iter->next = list->tail;
iter->direction = AL_START_TAIL;
}
/*}}}*/
listIter *listNext(listIter *iter)
/*{{{*/
{
listNode *current = iter->next;
if(current != NULL)
{
if(iter->direction == AL_START_HEAD)
{
iter->next = current->next;
}
else
{
iter->next = current->prev;
}
}
return current;
}
/*}}}*/
list *listDup(list *orign)
/*{{{*/
{
list *cpy;
listIter *iter;
listNode *node;
if((cpy = listCreate()) == NULL)
{
return NULL;
}
cpy->free = orign->free;
cpy->dup = orign->dup;
cpy->match = orign->match;
iter = listGetIterator(orign, AL_START_HEAD);
while((node = listNext(iter)) != NULL)
{
void *value;
if(cpy->dup)
{
value = cpy->dup(node->value);
if(value == NULL)
{
listRelease(cpy);
listReleaseIterator(iter);
return NULL;
}
}
else
{
value = node->value;
}
//出错只有一种情况,就是malloc失败了,return NULL;
if(listAddNodeTail(cpy, value) == NULL)
{
listRelease(cpy);
listReleaseIterator(iter);
return NULL;
}
}
listReleaseIterator(iter);
return cpy;
}
/*}}}*/
list *listSearchKey(list *list, void *key)
/*{{{*/
{
listIter *iter;
listNode *node;
iter = listGetIterator(list, AL_START_HEAD);
while((node = listNext(iter)) != NULL)
{
if(list->match)
{
if(list->match(node->value, key))
{
listReleaseIterator(iter);
return node;
}
}
else
{
//如果match函数不存在,理论上讲是比较不了
//这时候只需要看地址就行了
if(key == node->value)
{
listReleaseIterator(iter);
return node;
}
}
}
listReleaseIterator(iter);
return NULL;
}
/*}}}*/
/**
* index>=0 从0位置正向开始
* index<0 从最后一个位置开始
*/
listNode *listIndex(list *list, long index)
/*{{{*/
{
listNode *node;
listIter *iter;
if(index < 0)
{
index = -index;
iter = listGetIterator(list, AL_START_TAIL);
while(index-- && (node = listNext(iter));
}
else
{
index++;
iter = listGetIterator(list, AL_START_HEAD);
while(index-- && (node = listNext(iter));
}
return node;
}
/*}}}*/
void listRotate(list *list)
/*{{{*/
{
listNode *tail = list->tail;
if(listLength(list) <= 1) return;
list->tail = tail->prev;
list->tail->next = NULL;
tail->next = list->head;
list->head->prev = tail;
tail->prev = NULL;
list->head = tail;
}
/*}}}*/