-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlist.c
108 lines (80 loc) · 1.71 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
/*
* list.c
* XBolo
*
* Created by Robert Chrzanowski on 11/7/04.
* Copyright 2004 Robert Chrzanowski. All rights reserved.
*
*/
#include "list.h"
#include "errchk.h"
#include <stdlib.h>
#include <assert.h>
int initlist(struct ListNode *list) {
assert(list != NULL);
list->prev = NULL;
list->next = NULL;
list->ptr = NULL;
TRY
CLEANUP
ERRHANDLER(0, -1)
END
}
int addlist(struct ListNode *list, void *ptr) {
struct ListNode *node = NULL;
assert(list != NULL);
TRY
if ((node = (struct ListNode *)malloc(sizeof(struct ListNode))) == NULL) LOGFAIL(errno)
node->prev = list;
node->next = list->next;
node->prev->next = node;
if (node->next != NULL) {
node->next->prev = node;
}
node->ptr = ptr;
CLEANUP
switch (ERROR) {
case 0:
RETURN(0)
default:
if (node != NULL) {
free(node);
node = NULL;
}
RETERR(-1)
}
END
}
struct ListNode *removelist(struct ListNode *node, void (*releasefunc)(void *)) {
struct ListNode *next;
node->prev->next = node->next;
if (node->next != NULL) {
node->next->prev = node->prev;
}
next = node->next;
releasefunc(node->ptr);
free(node);
return next;
}
void clearlist(struct ListNode *list, void (*releasefunc)(void *)) {
struct ListNode *node, *next;
assert(list != NULL);
for (node = list->next; node != NULL; node = next) {
next = node->next;
releasefunc(node->ptr);
free(node);
}
list->next = NULL;
}
struct ListNode *nextlist(struct ListNode *node) {
assert(node != NULL);
return node->next;
}
struct ListNode *prevlist(struct ListNode *node) {
assert(node != NULL);
return node->prev;
}
void *ptrlist(struct ListNode *node) {
assert(node != NULL);
return node->ptr;
}