list.c
4.09 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#define TRUE 1
#define FALSE 0
void List_Init(tList *pList)
{
tPosition pos;
pList->nCount = 0;
pos = &pList->Header;
pos->pContent = NULL;
pos->pNext = pos;
pos->pPrev = pos;
}
int List_IsEmpty(tList *pList)
{
return (pList->nCount == 0);
}
int List_GetCount(tList *pList)
{
return (pList->nCount);
}
tPosition List_AddAfter(tList *pList, tPosition pos, void *pContent)
{
tPosition posNew;
posNew = (tPosition ) malloc(sizeof(tElement));
if (posNew == NULL) {
fprintf(stderr, "Error: out of memory!\n");
exit(1);
}
posNew->pContent = pContent;
posNew->pPrev = pos;
posNew->pNext = pos->pNext;
pos->pNext->pPrev = posNew;
pos->pNext = posNew;
pList->nCount++;
return (posNew);
}
tPosition List_AddBefore(tList *pList, tPosition pos, void *pContent)
{
return (List_AddAfter(pList, pos->pPrev, pContent));
}
tPosition List_AddHead(tList *pList, void *pContent)
{
return (List_AddAfter(pList, &pList->Header, pContent));
}
tPosition List_AddTail(tList *pList, void *pContent)
{
return (List_AddBefore(pList, &pList->Header, pContent));
}
tPosition List_GotoHead(tList *pList)
{
if (pList->nCount == 0)
return (NULL);
else
return (pList->Header.pNext);
}
tPosition List_GotoTail(tList *pList)
{
if (pList->nCount == 0)
return (NULL);
else
return (pList->Header.pPrev);
}
void *List_GetNext(tList *pList, tPosition *ppos)
{
tPosition pos;
tPosition posNext;
pos = *ppos;
if (pos == NULL)
return (NULL);
else if ((posNext = pos->pNext) == &pList->Header) {
*ppos = NULL;
return (pos->pContent);
} else {
*ppos = posNext;
return (pos->pContent);
}
}
void *List_GetPrev(tList *pList, tPosition *ppos)
{
tPosition pos;
tPosition posPrev;
pos = *ppos;
if (pos == NULL)
return (NULL);
else if ((posPrev = pos->pPrev) == &pList->Header) {
*ppos = NULL;
return (pos->pContent);
} else {
*ppos = posPrev;
return (pos->pContent);
}
}
void *List_RemoveAt(tList *pList, tPosition pos)
{
tPosition posPrev;
tPosition posNext;
void *pContent;
if (pos == NULL)
return (NULL);
else {
pContent = pos->pContent;
posNext = pos->pNext;
posPrev = pos->pPrev;
posNext->pPrev = posPrev;
posPrev->pNext = posNext;
free(pos);
pList->nCount--;
return (pContent);
}
}
void *List_RemoveHead(tList *pList)
{
if (pList->nCount == 0)
return (NULL);
else
return (List_RemoveAt(pList, pList->Header.pNext));
}
void *List_RemoveTail(tList *pList)
{
if (pList->nCount == 0)
return (NULL);
else
return (List_RemoveAt(pList, pList->Header.pPrev));
}
tPosition List_Find(tList *pList, void *pContent)
{
tPosition pos;
pos = pList->Header.pNext;
while (pos != &pList->Header) {
if (pos->pContent == pContent)
return (pos);
pos = pos->pNext;
}
return (NULL);
}
void *List_GetAt(tList *pList, tPosition pos)
{
if (pos == NULL)
return (NULL);
else
return (pos->pContent);
}
tPosition List_TraverseForward(tList *pList, tPosition pos, tVisitFunc *pVisitFunc, void *pArgs)
{
tPosition posPrev;
void *pContent;
while (pos != NULL) {
posPrev = pos;
pContent = List_GetNext(pList, &pos);
if ((*pVisitFunc)(pContent, pArgs) == 1)
return (posPrev);
}
return (NULL);
}
tPosition List_TraverseBackward(tList *pList, tPosition pos, tVisitFunc *pVisitFunc, void *pArgs)
{
tPosition posNext;
void *pContent;
while (pos != NULL) {
posNext = pos;
pContent = List_GetPrev(pList, &pos);
if ((*pVisitFunc)(pContent, pArgs) == 1)
return (posNext);
}
return (NULL);
}
void List_Sort(tList *pList, int (*comp)(const void *pContent1, const void *pContent2))
{
tPosition *array;
int i, nCount;
tPosition pos, posNew;
nCount = pList->nCount;
array = (tPosition *) malloc(nCount * sizeof(tPosition));
i = 0;
pos = pList->Header.pNext;
while (pos != &pList->Header) {
array[i++] = pos;
pos = pos->pNext;
}
qsort(array, nCount, sizeof(tPosition), comp);
pos = &pList->Header;
pos->pNext = pos;
pos->pPrev = pos;
for (i = 0; i < nCount; i++) {
posNew = array[i];
posNew->pPrev = pos;
posNew->pNext = pos->pNext;
pos->pNext->pPrev = posNew;
pos->pNext = posNew;
pos = posNew;
}
free(array);
}