-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.c
541 lines (495 loc) · 9.04 KB
/
list.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// List functions
/* Adapted from: Yu Zhang ([email protected]) */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "list.h"
static ListItr glistitr = NULL;
// new an error or a warn message
listNode
newlistNode(void *item)
{
listNode new;
NEW0(new);
new->item = item;
return new;
}
List
newList()
{
List new;
NEW0(new);
listNode newNode;
NEW0(newNode);
new -> first = newNode;
new -> last = newNode;
return new;
}
/**
* Links element item as first element of the list
*/
static void
linkFirst(List list, void *item)
{
listNode first = list->first;
listNode firstNext = first->next;
listNode newnode = newlistNode(item);
newnode->next = firstNext;
newnode->prev = first;
newnode->list = list;
if ( firstNext == NULL )
list->last = newnode;
else
firstNext->prev = newnode;
first -> next = newnode;
list->size ++;
}
/**
* Links element item as first element of the list
*/
static void
linkLast(List list, void *item)
{
listNode last = list->last;
listNode newnode = newlistNode(item);
newnode->prev = last;
newnode->next = NULL;
newnode->list = list;
last->next = newnode;
list->last = newnode;
list->size ++;
}
/**
* Inserts element item before non-NULL & non-default-first node succ
*/
static void
linkBefore(List list, void *item, listNode succ)
{
assert(succ != NULL);
listNode pred = succ->prev;
listNode newnode = newlistNode(item);
newnode->next = succ;
newnode->prev = succ->prev;
newnode->list = list;
succ->prev = newnode;
pred->next = newnode;
list->size ++;
}
/**
* Unlinks non-NULL & non-default-first node
*/
static void *
unlink(List list, listNode node)
{
assert(node != NULL);
listNode prev, next;
void *item;
item = node->item;
prev = node->prev;
next = node->next;
if (prev == NULL)
list->first = next;
else {
prev->next = next;
node->prev = NULL;
}
if (next == NULL)
list->last = prev;
else {
next->prev = prev;
node->next = NULL;
}
node->item = NULL;
free(node);
list->size--;
return item;
}
/**
* Returns the first element in this list
*/
void *
getFirst(List list)
{
listNode first = list->first->next;
if ( first == NULL ) {
printf("The list is empty!\n");
exit(-1);
}
return first->item;
}
/**
* Returns the last element in this list
*/
void *
getLast(List list)
{
listNode last = list->last;
if ( last == NULL || list->first->next == NULL) {
printf("The list is empty!\n");
exit(-1);
}
return last->item;
}
/**
* Removes and returns the first element from this list
*/
void *
removeFirst(List list)
{
listNode first = list->first->next;
if ( first == NULL ) {
printf("The list is empty!\n");
exit(-1);
}
return unlink(list, first);
}
/**
* Removes and returns the last element from this list
*/
void *
removeLast(List list)
{
listNode last = list->last;
if ( last == NULL || list->first->next == NULL ) {
printf("The list is empty!\n");
exit(-1);
}
return unlink(list, last);
}
/**
* Inserts the specified element at the beginning of the list
*/
void
addFirst(List list, void * item)
{
linkFirst(list, item);
}
/**
* Inserts the specified element to the end of the list
*/
void
addLast(List list, void * item)
{
linkLast(list, item);
}
/**
* Returns the index of the first occurence of the specified
* element in the list, or -1 if the list does not contain
* the element.
*/
int
indexof(List list, void *item)
{
int index = 0;
listNode node = list->first->next;
while (node != NULL) {
if ( (node->item == item) )
return index;
index ++;
node = node->next;
}
return -1;
}
/**
* Returns TRUE if the list contains the specified element.
*/
bool
listcontains(List list, void *item)
{
return indexof(list, item) != -1;
}
/**
* Returns the number of elements in the list
*/
int
listsize(List list)
{
return list->size;
}
/**
* Appends the specified element to the end of the list
*/
bool
listaddItem(List list, void * item)
{
linkLast(list, item);
return TRUE;
}
/**
* Removes the first occurrence of the specified element from
* the list, if it is present.
*/
bool
listremoveItem(List list, void *item)
{
listNode node = list->first->next;
while(node!=NULL) {
if (node->item == item) {
unlink(list, node);
return TRUE;
}
node = node->next;
}
return FALSE;
}
/**
* Removes all of the elements from the list
*/
void
listclear(List list, void destroyItem())
{
listNode node = list->first->next;
while (node != NULL) {
listNode next = node->next;
if (destroyItem != NULL)
destroyItem(&node->item);
node->item = NULL;
free(node);
node = next;
}
list->size = 0;
}
/**
* Returns the node at the specified element index
*/
listNode
listnode(List list, int index)
{
listNode node;
int i;
if ( index < (list->size >> 1)) {
node = list->first->next;
for ( i = 0; i < index; i++)
node = node->next;
return node;
} else {
node = list->last;
for ( i = list->size - 1; i > index; i--)
node = node->prev;
return node;
}
}
/**
* Returns the element at the specified position in the list
*/
void *
listget(List list, int index)
{
listNode node = listnode(list, index);
return node==NULL ? NULL : node->item;
}
/**
* Replaces the element at the specified position in the list
*/
void *
listset(List list, int index, void *item)
{
listNode node = listnode(list, index);
if ( node==NULL ) {
printf("Index %d is out of the bound!\n", index);
exit(-1);
}
void *oldval = node->item;
node->item = item;
return oldval;
}
/**
* Inserts the specified element at the specified position in the
* list
*/
void
listadd(List list, int index, void *item)
{
if ( index == list->size ) {
linkLast(list, item);
return;
}
listNode node = listnode(list, index);
if ( node==NULL ) {
printf("Index %d is out of the bound!\n", index);
exit(-1);
}
linkBefore(list, item, node);
}
/**
* Removes the element at the specified position in the list
*/
void *
listremove(List list, int index)
{
listNode node = listnode(list, index);
if ( node==NULL ) {
printf("Index %d is out of the bound!\n", index);
exit(-1);
}
return unlink(list, node);
}
/**
* Destroys the list
*/
void
destroyList(List *list, void destroyItem())
{
listclear(*list, destroyItem);
free((*list)->first);
free(*list);
*list = NULL;
}
/*************************************************************
* Functions for iterating elements
************************************************************/
/**
* Creates an iterator
*/
ListItr
newListItr(List list, int index)
{
ListItr new;
NEW0(new);
new->list = list;
new->next = listnode(list, index);
new->nextIndex = index;
return new;
}
/**
* Gets the singleton global List iterator
*/
ListItr
getGListItr(List list, int index)
{
if ( glistitr == NULL )
NEW0(glistitr);
glistitr->list = list;
glistitr->next = listnode(list, index);
glistitr->nextIndex = index;
return glistitr;
}
/**
* Resets the list iterator
*/
void
resetListItr(ListItr itr, List list, int index)
{
assert(itr != NULL);
itr->list = list;
itr->next = listnode(list, index);
itr->nextIndex = index;
}
/**
* Returns TRUE if there is next element
*/
bool
hasNext(ListItr itr)
{
return itr->nextIndex < itr->list->size;
}
/**
* Advance and returns the next element
*/
void *
nextItem(ListItr itr)
{
if ( !hasNext(itr) ) {
printf("Already reach the end of the list.\n");
return NULL;
}
itr->lastRet = itr->next;
itr->next = itr->next->next;
itr->nextIndex ++;
return itr->lastRet->item;
}
/**
* Returns TRUE if there is previous element
*/
bool
hasPrevious(ListItr itr)
{
return itr->nextIndex > 0;
}
/**
* Backward and returns the previous element
*/
void *
prevItem(ListItr itr)
{
if ( !hasPrevious(itr) ) {
printf("Already reach the beginning of the list.\n");
return NULL;
}
if ( itr->next == NULL )
itr->next = itr->list->last;
else
itr->next = itr->next->prev;
itr->lastRet = itr->next;
itr->nextIndex --;
return itr->lastRet->item;
}
/**
* Destroys the iterator
*/
void
destroyListItr(ListItr *itr)
{
free(*itr);
*itr = NULL;
}
/**
* Return next iterator
*/
ListItr nextItr (ListItr itr)
{
if ( !hasNext(itr) ) {
printf("Already reach the end of the list.\n");
return NULL;
}
ListItr newItr = newListItr(itr->list, itr->nextIndex+1);
return newItr;
}
/**
* Return previous iterator
*/
ListItr prevItr (ListItr itr)
{
if ( !hasPrevious(itr) ) {
printf("Already reach the beginning of the list.\n");
return NULL;
}
ListItr newItr = newListItr(itr->list, itr->nextIndex-11);
return newItr;
}
/**
* destroyItem example.
*/
void destroyItem(void* item) {
free(item);
}
/**
* iterList example.
*/
void iterList (ListItr itr, void hook())
{
while(hasNext(itr))
{
// what you want to do
void* item = nextItem(itr); //会自动往下走的
hook(item);
}
}
void removeNode(listNode node)
{
// 仅适用于prev一定存在的情况
listNode prev = node -> prev;
listNode next = node -> next;
prev -> next = next;
if(next != NULL) next -> prev = prev;
node -> item = NULL;
node -> prev = NULL;
node -> next = NULL;
((List)(node -> list)) -> size --;
free(node);
}
listNode getFirstNode(List list)
{
return list->first->next;
}