-
Notifications
You must be signed in to change notification settings - Fork 4
/
listOrder.c
584 lines (478 loc) · 13.5 KB
/
listOrder.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
#include <stdarg.h>
/* ------------------------------ 声明区域 --------------------------------- */
// list node
typedef struct node{
char* str;
struct node* next;
} T_NODE, *PT_NODE;
typedef enum returnType {
FAIL = 0,
SUCCESS,
} E_RETURN_TYPE;
typedef enum boolType {
FALSE = 0,
TRUE,
} E_BOOL_TYPE;
/* ------------------------------ 公共函数 --------------------------------- */
// MyPrintf == puts + format args
void MyPrintf(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
printf("\r\n");
}
// 创建新的存储空间, 拷贝字符串的子串
char* DupSubStr(char* str, int start, int end)
{
char* newStr = NULL;
// sub string len 开区间 计算子串长度
int len = end - start + 1;
// regarding ending zero
len++;
newStr = (char*)malloc( sizeof(char)*len );
if ( NULL == newStr )
{
return NULL;
}
newStr[len-1] = '\0';
strncpy(newStr, str+start, len-1);
return newStr;
}
void StopWithMsg(char* msg)
{
puts(msg);
assert(0);
}
/***********************************************************************
LIST fundemental function
************************************************************************/
void FreeList(PT_NODE ptHeadNode)
{
PT_NODE ptCurNode = ptHeadNode->next;
PT_NODE ptNextNode = NULL;
while(ptCurNode)
{
ptNextNode = ptCurNode->next;
if ( ptCurNode->str != NULL )
{
free(ptCurNode->str);
}
free(ptCurNode);
ptCurNode = ptNextNode;
}
}
//create a node with string str and insert node into list
int Insert2List(PT_NODE ptHeadNode, char* str)
{
PT_NODE ptNode = NULL;
PT_NODE ptFirst = NULL;
//MyPrintf("Insert2List str is %s", str);
ptNode = (PT_NODE) malloc(sizeof(T_NODE));
if ( ptNode == NULL )
{
return -1;
}
ptNode->str = str;
ptFirst = ptHeadNode->next;
ptHeadNode->next = ptNode;
ptNode->next = ptFirst;
}
// insert node into list head position
PT_NODE InsertNode2ListHead(PT_NODE ptHeadNode, PT_NODE ptNode)
{
PT_NODE ptFirst = NULL;
ptFirst = ptHeadNode->next;
ptHeadNode->next = ptNode;
ptNode->next = ptFirst;
return ptHeadNode;
}
void PrintList(PT_NODE ptHeadNode)
{
PT_NODE ptCurNode = NULL;
MyPrintf("---- list printing begin -----");
ptCurNode = ptHeadNode->next;
while(ptCurNode)
{
MyPrintf("node str is %s", ptCurNode->str);
ptCurNode = ptCurNode->next;
}
MyPrintf("---- list printing end -----");
}
void AssertList(PT_NODE ptHeadNode, char* strArr[]){
int index = 0;
PT_NODE ptCurNode = ptHeadNode->next;
while(ptCurNode)
{
if ( strArr[index] == NULL )
{
StopWithMsg("strArr reach top, but list not over.");
return;
}
if ( strcmp(ptCurNode->str, strArr[index]) == 0 )
{
//MyPrintf("List index[%d] str [%s] matched.", index, strArr[index]);
index++;
ptCurNode = ptCurNode->next;
}
else
{
StopWithMsg("List index str don't matched.");
return;
}
}
if ( strArr[index] != NULL )
{
StopWithMsg("AssertList failed.");
return;
}
else
{
//MyPrintf("match all");
}
}
// compare two string, if str1 is larger than str2, return true, otherwise false
E_BOOL_TYPE StrCmpLarger(char* str1, char* str2)
{
if ( !str1 || !str2 )
{
StopWithMsg("strcmplarger function argments ");
}
if ( strcmp( str1, str2 ) > 0 )
{
return TRUE;
}
return FALSE;
}
// compare two string, if str1 is less than str2, return true, otherwise false
E_BOOL_TYPE StrCmpLess(char* str1, char* str2)
{
if ( !str1 || !str2 )
{
StopWithMsg("strcmplarger function argments ");
}
if ( strcmp( str1, str2 ) < 0 )
{
return TRUE;
}
return FALSE;
}
typedef E_BOOL_TYPE (*STRCMPFUNC)(char* str1, char* str2);
/***********************************************************************
selecting sort method
************************************************************************/
// find specific node by strcmpfunc and detach it from list
PT_NODE FindAndDetachNode(PT_NODE ptHeadNode, STRCMPFUNC strcmpfunc )
{
PT_NODE ptTargetNode = NULL;
PT_NODE ptPreMax = NULL;
PT_NODE ptCurNode = NULL;
PT_NODE ptPreNode = NULL;
ptTargetNode = ptHeadNode->next;
ptPreMax = ptHeadNode;
ptCurNode = ptHeadNode->next;
ptPreNode = ptHeadNode;
while( ptCurNode )
{
// current node string is larger than specific node string, record it
if ( strcmpfunc(ptCurNode->str, ptTargetNode->str) )
{
ptTargetNode = ptCurNode;
ptPreMax = ptPreNode;
}
ptPreNode = ptCurNode;
ptCurNode = ptCurNode->next;
}
// specific node found, detach it from list
if ( ptTargetNode )
{
ptPreMax->next = ptTargetNode->next;
ptTargetNode->next = NULL;
}
// return specific node
return ptTargetNode;
}
// sort list by specific order by strcmpfunc
void SortListWithSelectMethod(PT_NODE ptHeadNode, STRCMPFUNC strcmpfunc)
{
T_NODE tNewHead = {NULL, NULL};
PT_NODE ptNode = NULL;
// selection sort method
while( TRUE )
{
// find target node and detach it from list
ptNode = FindAndDetachNode(ptHeadNode, strcmpfunc);
if ( !ptNode )
{
break;
}
// insert target node into new list from head
InsertNode2ListHead(&tNewHead, ptNode);
}
// reset head node to new list
ptHeadNode->next = tNewHead.next;
tNewHead.next = NULL;
}
void TestList_SelectionMethod()
{
T_NODE headNode = {NULL, NULL};
//insert node into list
Insert2List(&headNode, "first");
Insert2List(&headNode, "second");
// print list node
PrintList(&headNode);
// test list node order
char* strArr[] = {
"second",
"first",
NULL
};
AssertList(&headNode, strArr);
// order list, from small to big
SortListWithSelectMethod(&headNode, StrCmpLarger);
// print list node
PrintList(&headNode);
// test list node new order
char* strArr1[] = {
"first",
"second",
NULL
};
AssertList(&headNode, strArr1);
// order list, from big to small
SortListWithSelectMethod(&headNode, StrCmpLess);
// print list node
PrintList(&headNode);
// test list node new order
char* strArr2[] = {
"second",
"first",
NULL
};
AssertList(&headNode, strArr2);
FreeList(&headNode);
}
/***********************************************************************
bubble sort method
************************************************************************/
// bubble list by strcmpfunc and detach last from list
PT_NODE BubbleAndDetachLast(PT_NODE ptHeadNode, STRCMPFUNC strcmpfunc)
{
PT_NODE ptCurNode = NULL;
PT_NODE ptPreNode = NULL;
PT_NODE ptNextNode = NULL;
ptCurNode = ptHeadNode->next;
ptPreNode = ptHeadNode;
while( ptCurNode )
{
ptNextNode = ptCurNode->next;
// reach list tail
if ( !ptNextNode )
{
break;
}
// current node string is larger than next node string, swap it
if ( strcmpfunc(ptCurNode->str, ptNextNode->str) )
{
ptPreNode->next = ptNextNode;
ptCurNode->next = ptNextNode->next;
ptNextNode->next = ptCurNode;
// reset current node's previous node
ptPreNode = ptNextNode;
}
else
{
ptPreNode = ptCurNode;
ptCurNode = ptCurNode->next;
}
}
//detach current node from list
ptPreNode->next = NULL;
// current node is the last node, return it
return ptCurNode;
}
// sort list by specific order by strcmpfunc
void SortListWithBubbleMethod(PT_NODE ptHeadNode, STRCMPFUNC strcmpfunc)
{
T_NODE tNewHead = {NULL, NULL};
PT_NODE ptNode = NULL;
// selection sort method
while( TRUE )
{
// bubble list and detach last from list
ptNode = BubbleAndDetachLast(ptHeadNode, strcmpfunc);
if ( !ptNode )
{
break;
}
// insert max node into new list from head
InsertNode2ListHead(&tNewHead, ptNode);
}
// reset head node to new list
ptHeadNode->next = tNewHead.next;
tNewHead.next = NULL;
}
void TestList_BubbleMethod()
{
T_NODE headNode = {NULL, NULL};
//insert node into list
Insert2List(&headNode, "first");
Insert2List(&headNode, "second");
// print list node
PrintList(&headNode);
// test list node order
char* strArr[] = {
"second",
"first",
NULL
};
AssertList(&headNode, strArr);
// order list, from small to big
SortListWithBubbleMethod(&headNode, StrCmpLarger);
// print list node
PrintList(&headNode);
// test list node new order
char* strArr1[] = {
"first",
"second",
NULL
};
AssertList(&headNode, strArr1);
// order list, from big to small
SortListWithBubbleMethod(&headNode, StrCmpLess);
// print list node
PrintList(&headNode);
// test list node new order
char* strArr2[] = {
"second",
"first",
NULL
};
AssertList(&headNode, strArr2);
FreeList(&headNode);
}
/***********************************************************************
inserting sort method
************************************************************************/
PT_NODE DetachFirstNode(PT_NODE ptHeadNode)
{
PT_NODE ptFirstNode = NULL;
ptFirstNode = ptHeadNode->next;
// detach first node from list
if ( ptFirstNode )
{
ptHeadNode->next = ptFirstNode->next;
}
return ptFirstNode;
}
// insert node into list by inserting sort method
void InsertNode2ListByInsertMethod(PT_NODE ptHeadNode, PT_NODE ptNode, STRCMPFUNC strcmpfunc)
{
PT_NODE ptPrevNode = NULL;
PT_NODE ptCurNode = NULL;
ptPrevNode = ptHeadNode;
ptCurNode = ptPrevNode->next;
while( ptCurNode )
{
if ( strcmpfunc(ptCurNode->str, ptNode->str) )
{
ptPrevNode->next = ptNode;
ptNode->next = ptCurNode;
// insert ok, let's leave
break;
}
else
{
ptPrevNode = ptCurNode;
ptCurNode = ptCurNode->next;
}
}
// current node is NULL, previous node is last node of list,
// ie, insert not ok, the node shall be added to tail
if ( !ptCurNode )
{
ptPrevNode->next = ptNode;
ptNode->next = NULL;
}
}
// sort list by specific order by strcmpfunc
void SortListWithInsertMethod(PT_NODE ptHeadNode, STRCMPFUNC strcmpfunc)
{
T_NODE tNewHead = {NULL, NULL};
PT_NODE ptNode = NULL;
// selection sort method
while( TRUE )
{
// get first node from list
ptNode = DetachFirstNode(ptHeadNode);
if ( !ptNode )
{
break;
}
// insert the node into new list by inserting method
InsertNode2ListByInsertMethod(&tNewHead, ptNode, strcmpfunc);
}
// reset head node to new list
ptHeadNode->next = tNewHead.next;
tNewHead.next = NULL;
}
void TestList_InsertingMethod()
{
T_NODE headNode = {NULL, NULL};
//insert node into list
Insert2List(&headNode, "first");
Insert2List(&headNode, "second");
// print list node
PrintList(&headNode);
// test list node order
char* strArr[] = {
"second",
"first",
NULL
};
AssertList(&headNode, strArr);
// order list, from small to big
SortListWithInsertMethod(&headNode, StrCmpLarger);
// print list node
PrintList(&headNode);
// test list node new order
char* strArr1[] = {
"first",
"second",
NULL
};
AssertList(&headNode, strArr1);
// order list, from big to small
SortListWithInsertMethod(&headNode, StrCmpLess);
// print list node
PrintList(&headNode);
// test list node new order
char* strArr2[] = {
"second",
"first",
NULL
};
AssertList(&headNode, strArr2);
FreeList(&headNode);
}
/***********************************************************************
Test case area
************************************************************************/
// list 链表操作测试用例
void TestListCase()
{
//TestList_SelectionMethod();
//TestList_BubbleMethod();
TestList_InsertingMethod();
}
int main(void)
{
// test list function
TestListCase();
return 0;
}