/
LinkList.c
348 lines (314 loc) · 8.25 KB
/
LinkList.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
/* ************************************************************************
* Filename: LinkList.c
* Programmer: ZhuJinliang
* Description:
* Version: 1.0
* Created: 2011/11/03
* Function:
* Modified by ZhuJinliang 2011-11-8
* ************************************************************************/
#include "LinkList.h"
/* *************************************************************************
* Name: InitList
* Function: Initial the list, the head doesn't have data
* Argument:
* Return: PListNode pHead
* ************************************************************************/
PListNode InitList(void)
{
PListNode pHead = CreateNode();
if (pHead == NULL)
{
return FAIL;
}
else
{
pHead->data = pHead;
pHead->next = NULL;
}
return pHead;
}
/* *************************************************************************
* Name: CreateNode
* Function: request a room to creat a blank node
* Argument: void
* Return: PListNode
* ************************************************************************/
PListNode CreateNode(void)
{
PListNode pNode = (PListNode)malloc(sizeof(ListNode));
return pNode;
}
/* ************************************************************************
* Name: DeleteNode
* Function: Delete a node
* Argument: PListNode pHead, int position
* Return: int
* ************************************************************************/
int DeleteNode(PListNode pHead, int position)
{
PListNode newHead;
PListNode preNode,delNode;
if (pHead == NULL)
{
return FAIL;
}
else
{
if (position == 1)
{
if (pHead->next != NULL)
{
newHead = pHead->next;
free(pHead);
pHead = newHead;
}
else
{
free(pHead);
}
}
else
{
preNode = FindNode(pHead, (position - 1));
delNode = preNode->next;
if (delNode->next == NULL)
{
preNode->next = NULL;
free(delNode);
}
else // not the first or last node
{
preNode->next = delNode->next;
free(delNode);
}
}
}
return TRUE;
}
/* ************************************************************************
* Name: DeleteList
* Function: Delete a List
* Argument: PListNode pHead
* Return: int
* ************************************************************************/
int DeleteList(PListNode pHead)
{
PListNode bufNode = NULL;
PListNode curNode = NULL;
bufNode = pHead;
if (pHead == NULL)
{
return FAIL;
}
else
{
while (bufNode->next != NULL)
{
curNode = bufNode->next;
free(bufNode);
bufNode = curNode;
}
free(bufNode);
pHead = NULL;
}
return TRUE;
}
/* ************************************************************************
* Name: AddTail
* Function: add a node to the tail of the list
* Argument: PListNode pHead, void *data
* Return: int
* ***********************************************************************/
int AddTail(PListNode pHead, void *data)
{
PListNode node = NULL;
PListNode newNode;
node = pHead;
if (node == NULL)
{
return FAIL;
}
else
{
while ((node->next) != NULL) // find the tail
{
node = node->next;
}
}
newNode = CreateNode();
if (newNode == NULL)
{
return FAIL;
}
else
{
newNode->data = data;
newNode->next = NULL;
node->next = newNode;
}
return TRUE;
}
/* ************************************************************************
* Name: InsertAfter
* Function: insert a node after the node(position) in the list
* Argument: PListNode pHead, int position, void *data
* Return: int
* ***********************************************************************/
int InsertAfter(PListNode pHead, int position, void *data)
{
PListNode preNode = NULL;
PListNode curNode = NULL;
if (pHead == NULL)
{
return FAIL;
}
else
{
preNode = FindNode(pHead, position); // get the position of the node
curNode = CreateNode();
curNode->data = data;
curNode->next = preNode->next;
preNode->next = curNode;
}
return TRUE;
}
/* ************************************************************************
* Name: UpdateNode
* Function: Update the data of a node in the list
* Argument: PListNode pHead, int position, void *data
* Return: int
* ***********************************************************************/
int UpdateNode(PListNode pHead, int position, void *data)
{
PListNode curNode = NULL;
if (pHead == NULL)
{
return FAIL;
}
else
{
curNode = FindNode(pHead, position);
curNode->data = data;
}
return TRUE;
}
/* ************************************************************************
* Name: GetListCount
* Function: Get the count of list
* Argument: PListNode pHead
* Return: int the count of List
* ************************************************************************/
int GetListCount(PListNode pHead)
{
int count = 1;
PListNode node = pHead;
if (node == NULL)
{
return 0;
}
else
{
while ((node->next) != NULL)
{
count ++;
node = node->next;
}
}
return count;
}
/* ************************************************************************
* Name: FindNode
* Function: Find the node in list
* Argument: PListNode pHead
* Return: PListNode
* ************************************************************************/
PListNode FindNode(PListNode pHead, int position)
{
int listCount = 1;
PListNode node = pHead;
if (pHead == NULL)
{
return FAIL;
}
else
{
while (node->next != NULL )
{
if (position == listCount)
{
break;
}
listCount ++;
node = node->next;
}
return node;
}
}
/* ************************************************************************
* Name: TraverseList
* Function: traverse all the node in the list
* Argument: PListNode pHead, void (*print)(void *)
* Return: PListNode
* ************************************************************************/
PListNode TraverseList(PListNode pHead, void (*print)(void *))
{
PListNode node = NULL;
node = pHead;
if (node == NULL)
{
return FAIL;
}
else
{
node = pHead->next;
while (node != NULL)
{
(*print)(node->data); // 通过函数指针来调用函数
node = node->next;
}
}
}
/* ************************************************************************
* Name: SortList
* Function: sort the data from big to small
* Argument: PListNode pHead, int (*compare)(void *, void *)
* Return: int
* ************************************************************************/
int SortList(PListNode pHead, int (*compare)(void *, void *))
{
void *temp = NULL;
PListNode pre = NULL;
PListNode cur = NULL;
for (pre=pHead->next; pre->next!=NULL; pre=pre->next)
{
for (cur=pre->next; cur!=NULL; cur=cur->next)
{
if ((*compare)(pre->data, cur->data) < 0)
{
temp = pre->data;
pre->data = cur->data;
cur->data = temp;
}
}
}
}
/* ************************************************************************
* Name: SearchNode
* Function:
* Argument: PListNode pHead, void *data, int (*compare)(void *, void *)
* Return: int
* ************************************************************************/
PListNode SearchNode(PListNode pHead, void *data, int (*compare)(void *, void *))
{
PListNode node = pHead;
while (node)
{
if (!(*compare)(node->data, data))
{
return node;
}
node = node->next;
}
return FAIL;
}