-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
1259 lines (1182 loc) · 37.2 KB
/
main.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
============================================================================
Name : main.c
Author : morris
Version : 1.0
Copyright : Your copyright notice
Description :
============================================================================
*/
#include <loocSeqList.h>
#include <loocSingleList.h>
#include <loocDoubleList.h>
#include <loocCircularList.h>
#include <loocStack.h>
#include <loocQueue.h>
#include <loocHashMap.h>
#include <loocBinTree.h>
#include <loocBinSearchTree.h>
#include <loocHeap.h>
#include <loocAdjacencyGraph.h>
#include <loocLinkedGraph.h>
#include <loocDisjointSet.h>
#include <loocMatrix.h>
#include <loocString.h>
#include <lea.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define SEQLIST_LENGTH (10)
#define SINGLELIST_LENGTH (5)
/**
* 针对二叉树节点的操作
* @param node 当前二叉树节点
* @param args 参数
*/
static void actionPrint_BinTree(loocBinTreeNode* node, void* args) {
printf("%c ", *(char*) (node->_data));
}
/**
* 针对二叉查找树节点的操作
* @param node 当前二叉查找树节点
* @param args 参数
*/
static void actionPrint_BinSearchTree(loocBinSearchTreeNode* node, void* args) {
printf("%d ", *(int*) (node->_data));
}
/**
* 针对图的节点的操作
* @param node 当前图顶点节点
* @param args 参数
*/
static void actionPrint_Graph(void* node, void* args) {
printf("%c ", *(char*) node);
}
/**
* 散列函数(采用除数留余法计算散列地址)
* @param cthis 当前Hash表对象指针
* @param value 数据指针
* @return 返回数据的散列地址
*/
static int hash(loocHashMap* cthis, void* value) {
int data = *(int*) value;
return data % (cthis->_maxSize);
}
/**
* 二叉查找树的比较策略
* @param cthis 旧节点
* @param node 新节点
* @return 新节点的关键字大于旧节点的关键字就返回1,小于返回-1,等于返回0
*/
static int BST_compareStrategy(loocBinSearchTreeNode* cthis,
loocBinSearchTreeNode* node) {
int a = *(int*) cthis->_data;
int b = *(int*) node->_data;
if (a > b) {
return -1;
} else if (a < b) {
return 1;
} else {
return 0;
}
}
/**
* 堆的比较策略
* @param old 旧节点
* @param new 新节点
* @return 新节点的关键字大于旧节点的关键字就返回1,小于返回-1,等于返回0
*/
static int Heap_compareStrategy(void* old, void* new) {
int a = *(int*) old;
int b = *(int*) new;
if (a > b) {
return -1;
} else if (a < b) {
return 1;
} else {
return 0;
}
}
static void FloydDisplaySinglePath(
int path[][LOOC_DEFAULT_LOOCADJACENCYGRAPH_VERTEX], int i, int j) {
int k = path[i][j];
if (k != -1) {
FloydDisplaySinglePath(path, i, k);
printf("%d ", k);
FloydDisplaySinglePath(path, k, j);
}
}
/**
* 打印多源最短路径(每对顶点的最短路径)
* @param cthis 当前图对象指针
* @param D 保存每对顶点的最短路径的权值
* @param path 记录最短路径上中间点的前驱点
*/
static void FloydDisplayPaths(loocAdjacencyGraph* cthis,
int D[][LOOC_DEFAULT_LOOCADJACENCYGRAPH_VERTEX],
int path[][LOOC_DEFAULT_LOOCADJACENCYGRAPH_VERTEX]) {
int i, j;
for (i = 0; i < cthis->numV; i++) {
for (j = 0; j < cthis->numV; j++) {
if (D[i][j] != LOOC_GRAPH_NO_EDGE) {
printf("顶点%d到顶点%d的最短路径权值为%d\r\n", i, j, D[i][j]);
printf("%d ", i);
FloydDisplaySinglePath(path, i, j);
printf("%d\r\n", j);
}
}
}
}
/**
* 欲求定积分的表达式
*/
static double expression(double x) {
if (x != 0) {
return sin(x) / x;
} else {
return 1;
}
}
int main(int argc, char **argv) {
int i = 0, j = 0;
/**
* 1. 顺序表的操作
*/
printf("****************loocSeqList****************\r\n");
/* 新建顺序表对象 */
loocSeqList* seqList = loocSeqList_new(looc_file_line);
/* 初始化顺序表,并分配好内存空间,顺序表大小为10,元素类型为int */
seqList->init(seqList, SEQLIST_LENGTH, sizeof(int));
/* 向顺序表中插入10个数据 */
for (i = 0; i < SEQLIST_LENGTH; i++) {
seqList->insert(seqList, (void*) &i);
}
/* 打印顺序表中的元素 */
for (i = 0; i < seqList->length; i++) {
printf("%d ", *(int*) (seqList->getAt(seqList, i)));
}
printf("\r\n");
/* 删除顺序表中第9个元素 */
seqList->removeAt(seqList, 9);
/* 删除顺序表中第0个元素 */
seqList->removeAt(seqList, 0);
/* 打印顺序表中的元素 */
for (i = 0; i < seqList->length; i++) {
printf("%d ", *(int*) (seqList->getAt(seqList, i)));
}
printf("\r\n");
/* 将顺序表中第5个元素修改66 */
i = 66;
seqList->modifyAt(seqList, 5, &i);
/* 打印顺序表中的元素 */
for (i = 0; i < seqList->length; i++) {
printf("%d ", *(int*) (seqList->getAt(seqList, i)));
}
printf("\r\n");
/* 释放顺序表内存空间 */
loocSeqList_delete(seqList);
/* 报告内存泄漏情况 */
looc_report();
/**
* 2. 单向链表的操作
*/
printf("****************loocSingleList****************\r\n");
i = 11;
/* 创建单向链表对象 */
loocSingleList* singleList = loocSingleList_new(looc_file_line);
/* 创建单链表节点对象 */
loocSingleListNode* singleListNode = loocSingleListNode_new(looc_file_line);
/* 节点对象初始化 */
singleListNode->init(singleListNode, sizeof(int), (void*) &i);
/* 初始化单向链表,创建头结点,分配内存空间,结点元素为int类型 */
singleList->init(singleList, sizeof(int), singleListNode);
/* 插入整形数据,插入链表头 */
i = 22;
singleList->insertAt(singleList, 0, (void*) &i);
i = 33;
singleList->insertAt(singleList, 1, (void*) &i);
i = 44;
singleList->insertAt(singleList, 2, (void*) &i);
i = 55;
singleList->insertAt(singleList, 3, (void*) &i);
/* 删除指定位置节点 */
singleList->removeAt(singleList, 4);
/* 打印单向链表中的元素 */
for (i = 0; i < singleList->length; i++) {
printf("%d ", *(int*) (singleList->getAt(singleList, i)));
}
printf("\r\n");
printf("circle appears at %p\r\n", singleList->haveCircle(singleList));
/* 释放单向链表内存空间 */
loocSingleList_delete(singleList);
/* 报告内存泄漏情况 */
looc_report();
/**
* 3. 双向链表的操作
*/
printf("****************loocDoubleList****************\r\n");
i = 'a';
/* 创建双向链表对象 */
loocDoubleList* doubleList = loocDoubleList_new(looc_file_line);
/* 创建双向链表节点对象 */
loocDoubleListNode* doubleListNode = loocDoubleListNode_new(looc_file_line);
/* 节点对象初始化 */
doubleListNode->init(doubleListNode, sizeof(int), (void*) &i);
/* 初始化双向链表 */
doubleList->init(doubleList, sizeof(int), doubleListNode);
/* 插入数据 */
i = 'b';
doubleList->insertAt(doubleList, 0, (void*) &i);
i = 'c';
doubleList->insertAt(doubleList, 1, (void*) &i);
i = 'd';
doubleList->insertAt(doubleList, 2, (void*) &i);
i = 'e';
doubleList->insertAt(doubleList, 3, (void*) &i);
/* 删除指定位置节点 */
doubleList->removeAt(doubleList, 0);
doubleList->removeAt(doubleList, 3);
/* 打印双向链表中的数据 */
for (i = 0; i < doubleList->length; i++) {
printf("%c ", *(char*) (doubleList->getAt(doubleList, i)));
}
printf("\r\n");
/* 释放双向链表内存 */
loocDoubleList_delete(doubleList);
/* 报告内存泄漏情况 */
looc_report();
/**
* 4. 循环链表的操作
*/
printf("****************loocCircularList****************\r\n");
i = 17;
/* 创建循环链表对象 */
loocCircularList* circularList = loocCircularList_new(looc_file_line);
/* 创建循环链表节点对象 */
loocCircularListNode* circularListNode = loocCircularListNode_new(
looc_file_line);
/* 节点对象初始化 */
circularListNode->init(circularListNode, sizeof(int), (void*) &i);
/* 初始化循环链表 */
circularList->init(circularList, sizeof(int), circularListNode);
/* 插入数据 */
i = 18;
circularList->insertAt(circularList, 0, (void*) &i);
i = 19;
circularList->insertAt(circularList, 1, (void*) &i);
i = 20;
circularList->insertAt(circularList, 2, (void*) &i);
i = 21;
circularList->insertAt(circularList, 3, (void*) &i);
/* 删除指定位置节点 */
circularList->removeAt(circularList, 0);
circularList->removeAt(circularList, 3);
/* 打印循环链表中的数据 */
for (i = 0; i < circularList->length; i++) {
printf("%d ", *(char*) (circularList->getAt(circularList, i)));
}
printf("\r\n");
/* 释放循环链表内存 */
loocCircularList_delete(circularList);
/* 报告内存泄漏情况 */
looc_report();
/**
* 5. 栈的操作
*/
printf("****************loocStack****************\r\n");
/* 创建栈对象 */
loocStack* stack = loocStack_new(looc_file_line);
/* 初始化栈 */
stack->init(stack, 10, sizeof(int));
/* 压栈操作 */
for (i = 0; i < 10; i++) {
stack->push(stack, (void*) &i);
}
/* 出栈操作 */
for (i = 0; i < 10; i++) {
printf("%d ", *(int*) stack->pop(stack));
}
printf("\r\n");
/* 释放栈内存空间 */
loocStack_delete(stack);
/* 报告内存泄漏情况 */
looc_report();
/**
* 6. 队列的操作
*/
printf("****************loocQueue****************\r\n");
/* 创建队列对象 */
loocQueue* queue = loocQueue_new(looc_file_line);
/* 初始化对列 */
queue->init(queue, 10, sizeof(int));
/* 入队操作 */
for (i = 0; i < 15; i++) {
queue->enqueue(queue, (void*) &i);
}
/* 出队操作 */
for (i = 0; i < 10; i++) {
printf("%d ", *(int*) queue->dequeue(queue));
}
printf("\r\n");
/* 释放队列内存空间 */
loocQueue_delete(queue);
/* 报告内存泄漏情况 */
looc_report();
/**
* 7. 哈希表的操作
*/
printf("****************loocHashMap****************\r\n");
/* 创建Hash表对象 */
loocHashMap* hashMap = loocHashMap_new(looc_file_line);
/* 初始化Hash表对象 */
hashMap->init(hashMap, 13, sizeof(int), hash);
/* 依次插入元素 */
i = 66;
hashMap->insert(hashMap, (void*) &i);
i = 32;
hashMap->insert(hashMap, (void*) &i);
i = 0;
hashMap->insert(hashMap, (void*) &i);
i = 478;
hashMap->insert(hashMap, (void*) &i);
i = 11;
hashMap->insert(hashMap, (void*) &i);
i = 23;
hashMap->insert(hashMap, (void*) &i);
i = 43;
hashMap->insert(hashMap, (void*) &i);
i = 55;
hashMap->insert(hashMap, (void*) &i);
i = 67;
hashMap->insert(hashMap, (void*) &i);
i = 108;
hashMap->insert(hashMap, (void*) &i);
i = 230;
hashMap->insert(hashMap, (void*) &i);
i = 223;
hashMap->insert(hashMap, (void*) &i);
i = 10;
hashMap->insert(hashMap, (void*) &i);
/* 输出Hash表中元素 */
for (i = 0; i < 13; i++) {
printf("{%d:%d} ", i, *(int*) hashMap->getAt(hashMap, i));
}
printf("\r\n");
/* 打印Hash表有效长度 */
printf("Total %d elements\r\n", hashMap->length);
/* 查找元素55 */
i = 55;
printf("Position of 55: %d\r\n", hashMap->search(hashMap, (void*) &i));
/* 释放Hash表内存空间 */
loocHashMap_delete(hashMap);
/* 报告内存泄漏情况 */
looc_report();
/**
* 8. 二叉树的操作
*/
printf("****************loocBinTree****************\r\n");
i = 'A';
/* 创建二叉树对象 */
loocBinTree* binTree = loocBinTree_new(looc_file_line);
/* 创建二叉树节点对象 */
loocBinTreeNode* binTreeNode = loocBinTreeNode_new(looc_file_line);
/* 节点对象初始化 */
binTreeNode->init(binTreeNode, sizeof(int), (void*) &i);
/* 二叉树对象初始化,指定根节点 */
binTree->init(binTree, sizeof(int), binTreeNode);
/* 增子节点 */
i = 'B';
binTreeNode->setLeftChild(binTreeNode, (void*) &i);
i = 'C';
binTreeNode->setRightChild(binTreeNode, (void*) &i);
i = 'D';
binTreeNode->lChild->setLeftChild(binTreeNode->lChild, (void*) &i);
i = 'E';
binTreeNode->lChild->setRightChild(binTreeNode->lChild, (void*) &i);
i = 'F';
binTreeNode->rChild->setLeftChild(binTreeNode->rChild, (void*) &i);
i = 'G';
binTreeNode->rChild->setRightChild(binTreeNode->rChild, (void*) &i);
i = 'H';
binTreeNode->lChild->lChild->setLeftChild(binTreeNode->lChild->lChild,
(void*) &i);
i = 'I';
binTreeNode->lChild->lChild->setRightChild(binTreeNode->lChild->lChild,
(void*) &i);
i = 'J';
binTreeNode->lChild->rChild->setRightChild(binTreeNode->lChild->rChild,
(void*) &i);
/* 前序遍历打印节点 */
binTree->preOrder(binTree, actionPrint_BinTree, NULL);
printf("\r\n");
/* 中序遍历打印节点 */
binTree->inOrder(binTree, actionPrint_BinTree, NULL);
printf("\r\n");
/* 后序遍历打印节点 */
binTree->postOrder(binTree, actionPrint_BinTree, NULL);
printf("\r\n");
/* 层序遍历打印节点 */
binTree->layerOrder(binTree, actionPrint_BinTree, NULL);
printf("\r\n");
/* 打印已知节点的父节点 */
printf("node E's Parent is :%c\r\n",
*(char*) (binTree->getParent(binTree, binTree->root->lChild->rChild)->_data));
/* 打印已知节点的兄弟节点 */
printf("node E's Brother is :%c\r\n",
*(char*) (binTree->getBrother(binTree,
binTree->root->lChild->rChild)->_data));
/* 打印树的高度 */
printf("Height of Tree: %d\r\n", binTree->getHeight(binTree));
/* 打印指定节点的深度 */
printf("Depth of node D: %d\r\n",
binTree->getDepthOfNode(binTree, binTree->root->lChild->lChild));
/* 释放二叉树对象内存空间 */
loocBinTree_delete(binTree);
/* 报告内存泄漏情况 */
looc_report();
/**
* 9. 二叉查找树的操作
*/
printf("****************loocBinSearchTree****************\r\n");
i = 100;
/* 创建二叉查找树节点对象 */
loocBinSearchTreeNode* binSearchTreeNode = loocBinSearchTreeNode_new(
looc_file_line);
/* 初始化二叉查找树节点 */
binSearchTreeNode->init(binSearchTreeNode, sizeof(int), (void*) &i);
/* 创建二叉查找树对象 */
loocBinSearchTree* binSearchTree = loocBinSearchTree_new(looc_file_line);
/* 初始化二叉查找树,并设置根节点 */
binSearchTree->init(binSearchTree, sizeof(int), binSearchTreeNode,
BST_compareStrategy);
/* 往二叉查找树中插入数据 */
i = 77;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 103;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 191;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 88;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 143;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 43;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 51;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 152;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 6;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 200;
binSearchTree->insert(binSearchTree, (void*) &i);
i = 199;
binSearchTree->insert(binSearchTree, (void*) &i);
/* 中序遍历打印节点,即排序 */
binSearchTree->inOrder(binSearchTree, actionPrint_BinSearchTree, NULL);
printf("\r\n");
/* 查询操作 */
i = 100;
binSearchTreeNode = binSearchTree->search(binSearchTree, (void*) &i);
if (binSearchTreeNode) {
if (binSearchTreeNode->parent) {
printf("Parent of node %d is node %d\r\n", i,
*(int*) (binSearchTreeNode->parent->_data));
}
/* 删除指定节点 */
binSearchTree->deleteNode(binSearchTree, binSearchTreeNode);
}
/* 中序遍历打印节点,即排序 */
binSearchTree->inOrder(binSearchTree, actionPrint_BinSearchTree, NULL);
printf("\r\n");
/* 打印最大最小值 */
printf("Max:%d\tMin:%d\r\n",
*(int*) binSearchTree->getMaxNode(binSearchTree)->_data,
*(int*) binSearchTree->getMinNode(binSearchTree)->_data);
/* 释放二叉查找树对象内存空间 */
loocBinSearchTree_delete(binSearchTree);
/* 报告内存泄漏情况 */
looc_report();
/**
* 11. 堆的操作
*/
printf("****************loocHeap****************\r\n");
/* 创建堆对象 */
loocHeap* heap = loocHeap_new(looc_file_line);
/* 初始化堆对象(最小堆),存储int值 */
heap->init(heap, LOOC_MIN_HEAP, 10, sizeof(int), Heap_compareStrategy);
/* 插入数据 */
i = 77;
heap->insert(heap, (void*) &i);
i = 101;
heap->insert(heap, (void*) &i);
i = 98;
heap->insert(heap, (void*) &i);
i = 43;
heap->insert(heap, (void*) &i);
i = 110;
heap->insert(heap, (void*) &i);
i = 140;
heap->insert(heap, (void*) &i);
for (i = 0; i < 6; i++) {
/* 打印堆中的根节点元素 */
printf("%d\r\n", *(int*) (heap->getRoot(heap)));
/* 删除堆中的根节点元素 */
heap->deleteRoot(heap);
}
/* 释放堆对象内存空间 */
loocHeap_delete(heap);
/* 报告内存泄露情况 */
looc_report();
/**
* 12. 图(邻接矩阵存储)的操作
*/
printf("****************loocAdjacencyGraph****************\r\n");
/* 创建图对象 */
loocAdjacencyGraph* adjGraph = loocAdjacencyGraph_new(looc_file_line);
/* 初始化有向图,顶点元素为int数,最多10个顶点 */
adjGraph->init(adjGraph, 10, sizeof(int), 1);
/* 增加顶点 */
i = 'A';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'B';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'C';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'D';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'E';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'F';
adjGraph->addVertex(adjGraph, (void*) &i);
/* 增加边 */
adjGraph->insertEdge(adjGraph, 0, 1, 6);
adjGraph->insertEdge(adjGraph, 0, 2, 3);
adjGraph->insertEdge(adjGraph, 2, 1, 2);
adjGraph->insertEdge(adjGraph, 1, 3, 5);
adjGraph->insertEdge(adjGraph, 2, 3, 3);
adjGraph->insertEdge(adjGraph, 3, 5, 3);
adjGraph->insertEdge(adjGraph, 3, 4, 2);
adjGraph->insertEdge(adjGraph, 2, 4, 4);
adjGraph->insertEdge(adjGraph, 4, 5, 5);
/* 拓扑排序 */
int topOrder_adj[10];
adjGraph->topologySort(adjGraph, topOrder_adj);
for (i = 0; i < adjGraph->numV; i++) {
printf("%d\t", topOrder_adj[i]);
}
printf("\r\n");
/* 输出v0到各顶点的最短路径 */
int dist[10];
int path[10];
int s = 0;
adjGraph->Dijkstra(adjGraph, s, dist, path); //Dijkstra算法
for (i = 0; i < adjGraph->numV; i++) {
int stack[10];
int top = 0;
stack[top++] = i; //终点
int tempV = path[i]; //终点的前驱点
/* 循环知道没有前驱点或者前驱点是源点s */
while (tempV >= 0 && tempV != s) {
stack[top++] = tempV;
tempV = path[tempV];
}
stack[top] = s; //起点
/* 从起点开始输出路径 */
for (j = top; j >= 0; j--) {
if (j) {
printf("%d-->", stack[j]);
} else {
printf("%d:路径长度%d\r\n", stack[j], dist[i]);
}
}
}
/* 多源最短路径 */
int D[10][10];
int pathij[10][10];
adjGraph->Floyd(adjGraph, D, pathij);
FloydDisplayPaths(adjGraph, D, pathij);
/* 计算v3的出度 */
printf("out degree of node:%d\r\n", adjGraph->outDegree(adjGraph, 3));
/* 删除边 <v0,v1>*/
adjGraph->deleteEdge(adjGraph, 0, 1);
/* 计算v5的入度 */
printf("in degree of node:%d\r\n", adjGraph->inDegree(adjGraph, 5));
/* 深度优先遍历,从0号顶点开始 */
adjGraph->DFS(adjGraph, 0, actionPrint_Graph, NULL);
printf("\r\n");
/* 广度优先遍历,从0号节点开始 */
adjGraph->BFS(adjGraph, 0, actionPrint_Graph, NULL);
printf("\r\n");
/* 释放图对象内存空间 */
loocAdjacencyGraph_delete(adjGraph);
/* 报告内存泄露情况 */
looc_report();
/**
* 13. 图(邻接表存储)的操作
*/
printf("****************loocLinkedGraph****************\r\n");
/* 创建图对象 */
loocLinkedGraph* linkGraph = loocLinkedGraph_new(looc_file_line);
/* 初始化有向图,顶点元素为int数,最多10个顶点 */
linkGraph->init(linkGraph, 10, sizeof(int), 1);
/* 增加顶点 */
i = 'A';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'B';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'C';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'D';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'E';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'F';
linkGraph->addVertex(linkGraph, (void*) &i);
/* 增加边 */
linkGraph->insertEdge(linkGraph, 0, 1, 6);
linkGraph->insertEdge(linkGraph, 0, 2, 3);
linkGraph->insertEdge(linkGraph, 2, 1, 2);
linkGraph->insertEdge(linkGraph, 1, 3, 5);
linkGraph->insertEdge(linkGraph, 2, 3, 3);
linkGraph->insertEdge(linkGraph, 3, 5, 3);
linkGraph->insertEdge(linkGraph, 3, 4, 2);
linkGraph->insertEdge(linkGraph, 2, 4, 4);
linkGraph->insertEdge(linkGraph, 4, 5, 5);
/* 拓扑排序 */
int topOrder_link[10];
linkGraph->topologySort(linkGraph, topOrder_link);
for (i = 0; i < linkGraph->numV; i++) {
printf("%d\t", topOrder_link[i]);
}
printf("\r\n");
/* 输出v0到各顶点的最短路径 */
s = 0;
linkGraph->Dijkstra(linkGraph, s, dist, path); //Dijkstra算法
for (i = 0; i < linkGraph->numV; i++) {
int stack[10];
int top = 0;
stack[top++] = i; //终点
int tempV = path[i]; //终点的前驱点
/* 循环知道没有前驱点或者前驱点是源点s */
while (tempV >= 0 && tempV != s) {
stack[top++] = tempV;
tempV = path[tempV];
}
stack[top] = s; //起点
/* 从起点开始输出路径 */
for (j = top; j >= 0; j--) {
if (j) {
printf("%d-->", stack[j]);
} else {
printf("%d:路径长度%d\r\n", stack[j], dist[i]);
}
}
}
/* 多源最短路径 */
linkGraph->Floyd(linkGraph, D, pathij);
FloydDisplayPaths(linkGraph, D, pathij);
/* 计算v3的出度 */
printf("out degree of node:%d\r\n", linkGraph->outDegree(linkGraph, 3));
/* 删除边 <v0,v1>*/
linkGraph->deleteEdge(linkGraph, 0, 1);
/* 计算v5的入度 */
printf("in degree of node:%d\r\n", linkGraph->inDegree(linkGraph, 5));
/* 深度优先遍历,从0号顶点开始 */
linkGraph->DFS(linkGraph, 0, actionPrint_Graph, NULL);
printf("\r\n");
/* 广度优先遍历,从0号节点开始 */
linkGraph->BFS(linkGraph, 0, actionPrint_Graph, NULL);
printf("\r\n");
/* 释放图对象内存空间 */
loocLinkedGraph_delete(linkGraph);
/* 报告内存泄露情况 */
looc_report();
/**
* 14. 并查集的操作
*/
printf("****************loocDisjointSet****************\r\n");
/* 创建并查集对象 */
loocDisjointSet* set = loocDisjointSet_new(looc_file_line);
/* 初始化并查集对象,存储int值 ,最多10个数据*/
set->init(set, 10, sizeof(int));
/* 插入数据 */
for (i = 1; i <= 10; i++) {
set->insert(set, (void*) &i);
}
/* 并操作 */
j = 6;
i = 9;
set->Union(set, (void*) &i, (void*) &j);
i = 10;
set->Union(set, (void*) &i, (void*) &j);
j = 3;
i = 5;
set->Union(set, (void*) &i, (void*) &j);
i = 8;
set->Union(set, (void*) &i, (void*) &j);
j = 1;
i = 2;
set->Union(set, (void*) &i, (void*) &j);
i = 4;
set->Union(set, (void*) &i, (void*) &j);
i = 7;
set->Union(set, (void*) &i, (void*) &j);
i = 3;
set->Union(set, (void*) &i, (void*) &j);
/* 打印并查集 */
for (i = 0; i < 10; i++) {
printf("data:%d,parent:%d\r\n",
*(int*) (set->data_pool + i * set->_elementSize),
set->parent[i]);
}
/* 释放图对象内存空间 */
loocDisjointSet_delete(set);
/* 报告内存泄露情况 */
looc_report();
/* 算法测试 */
/* 判断数组是否是某个二叉查找树的后序遍历 */
printf("****************verifySquenceOfBST****************\r\n");
int test_squence[] = { 5, 7, 6, 9, 11, 10, 8 };
printf("verifySquenceOfBST %s\r\n",
verifySquenceOfBST(test_squence, 7) ? "pass" : "not pass");
/* 数制转换 */
char result[16];
transNum(76374, 16, result);
printf("%s\r\n", result);
transNum(234, 2, result);
printf("%s\r\n", result);
/* 大小端模式 */
printf("Machine %s little endian\r\n", isLittleEndian() ? "is" : "isn't");
/* 判断完全平方数 */
i = 144;
printf("%d %s Perfect Square\r\n", i, isPerfectSquare(i) ? "is" : "isn't");
/* 计算斐波那契数 */
i = 5;
printf("第%d个斐波那契数:%d\r\n", i, Fibonacci(i));
/* 插入排序 */
int data1[10] = { 45, 43, 23, 76, 897, 567, 32, 0, 24, 43 };
insertSort(data1, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data1[i]);
}
printf("\r\n");
/* 冒泡排序 */
int data2[10] = { 23, 43, 23, 19, 87, 65, 78, 90, 7, 10 };
bubbleSort(data2, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data2[i]);
}
printf("\r\n");
/* 快速排序 */
int data3[10] = { 23, 32, 76, 43, 11, 88, 23, 76, 23, 90 };
quickSort(data3, 0, 9);
for (i = 0; i < 10; i++) {
printf("%d\t", data3[i]);
}
printf("\r\n");
/* 归并排序 */
int data4[10] = { 32, 26, 15, 89, 87, 100, 654, 54, 33, 26 };
mergeSort(data4, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data4[i]);
}
printf("\r\n");
/* 希尔排序 */
int data5[10] = { 32, 26, 15, 89, 87, 100, 654, 54, 33, 26 };
shellSort(data5, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data5[i]);
}
printf("\r\n");
/* 选择排序 */
int data6[10] = { 32, 26, 15, 89, 87, 100, 654, 54, 33, 26 };
selectionSort(data6, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data6[i]);
}
printf("\r\n");
/* 堆排序 */
int data7[10] = { 32, 26, 15, 89, 87, 100, 654, 54, 33, 26 };
heapSort(data7, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data7[i]);
}
printf("\r\n");
/* 计数排序 */
int data8[10] = { 30, 54, 87, 98, 86, 34, 25, 55, 41, 67 };
countingSort(data8, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data8[i]);
}
printf("\r\n");
/* 基数排序 */
int data9[10] = { 35, 64, 88, 87, 896, 354, 15, 64, 1, 123 };
radixSort(data9, 10, 3, 10);
for (i = 0; i < 10; i++) {
printf("%d\t", data9[i]);
}
printf("\r\n");
/* 二分查找 */
printf("key:88@ %d\r\n", binSearch(data9, 10, 88));
/* 最小生成树算法Prim */
/* 邻接矩阵存储 */
/* 创建图对象 */
adjGraph = loocAdjacencyGraph_new(looc_file_line);
/* 初始化无向图,顶点元素为int类型,最多10个顶点 */
adjGraph->init(adjGraph, 10, sizeof(int), 0);
/* 增加顶点 */
i = 'A';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'B';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'C';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'D';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'E';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'F';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'G';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'H';
adjGraph->addVertex(adjGraph, (void*) &i);
i = 'I';
adjGraph->addVertex(adjGraph, (void*) &i);
/* 增加边 */
adjGraph->insertEdge(adjGraph, 0, 1, 4);
adjGraph->insertEdge(adjGraph, 0, 7, 8);
adjGraph->insertEdge(adjGraph, 1, 2, 8);
adjGraph->insertEdge(adjGraph, 1, 7, 11);
adjGraph->insertEdge(adjGraph, 2, 3, 7);
adjGraph->insertEdge(adjGraph, 2, 5, 4);
adjGraph->insertEdge(adjGraph, 2, 8, 2);
adjGraph->insertEdge(adjGraph, 3, 4, 9);
adjGraph->insertEdge(adjGraph, 3, 5, 14);
adjGraph->insertEdge(adjGraph, 4, 5, 10);
adjGraph->insertEdge(adjGraph, 5, 6, 2);
adjGraph->insertEdge(adjGraph, 6, 7, 1);
adjGraph->insertEdge(adjGraph, 6, 8, 6);
/* 存放最小生成树 */
loocAdjacencyGraph* MST_adj = loocAdjacencyGraph_new(looc_file_line);
MST_adj->init(MST_adj, adjGraph->numV, sizeof(int), adjGraph->check);
/* 调用最小生成树算法Prim或者Kruskal */
adjGraph->Kruskal(adjGraph, MST_adj);
/* 打印最小生成树 */
for (i = 0; i < MST_adj->numV; i++) {
for (j = 0; j < MST_adj->numV; j++) {
int edgeValue = MST_adj->getValueOfEdge(MST_adj, i, j);
if (edgeValue < LOOC_GRAPH_NO_EDGE) {
printf("%d\t", edgeValue);
} else {
printf("-\t");
}
}
printf("\r\n");
}
/* 释放图对象内存空间 */
loocAdjacencyGraph_delete(adjGraph);
loocAdjacencyGraph_delete(MST_adj);
/* 报告内存泄露情况 */
looc_report();
/* 邻接链表存储 */
linkGraph = loocLinkedGraph_new(looc_file_line);
/* 初始化无向图,顶点元素为int数,最多10个顶点 */
linkGraph->init(linkGraph, 10, sizeof(int), 0);
/* 增加顶点 */
i = 'A';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'B';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'C';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'D';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'E';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'F';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'G';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'H';
linkGraph->addVertex(linkGraph, (void*) &i);
i = 'I';
linkGraph->addVertex(linkGraph, (void*) &i);
/* 增加边 */
linkGraph->insertEdge(linkGraph, 0, 1, 4);
linkGraph->insertEdge(linkGraph, 0, 7, 8);
linkGraph->insertEdge(linkGraph, 1, 2, 8);
linkGraph->insertEdge(linkGraph, 1, 7, 11);
linkGraph->insertEdge(linkGraph, 2, 3, 7);
linkGraph->insertEdge(linkGraph, 2, 5, 4);
linkGraph->insertEdge(linkGraph, 2, 8, 2);
linkGraph->insertEdge(linkGraph, 3, 4, 9);
linkGraph->insertEdge(linkGraph, 3, 5, 14);
linkGraph->insertEdge(linkGraph, 4, 5, 10);
linkGraph->insertEdge(linkGraph, 5, 6, 2);
linkGraph->insertEdge(linkGraph, 6, 7, 1);
linkGraph->insertEdge(linkGraph, 6, 8, 6);
/* 存放最小生成树 */
loocLinkedGraph* MST_link = loocLinkedGraph_new(looc_file_line);
MST_link->init(MST_link, linkGraph->numV, sizeof(int), linkGraph->check);
/* 调用最小生成树算法Prim或者Kruskal */
linkGraph->Kruskal(linkGraph, MST_link);
/* 打印最小生成树 */
for (i = 0; i < MST_link->numV; i++) {
for (j = 0; j < MST_link->numV; j++) {
int edgeValue = MST_link->getValueOfEdge(MST_link, i, j);
if (edgeValue < LOOC_GRAPH_NO_EDGE) {
printf("%d\t", edgeValue);
} else {
printf("-\t");
}
}
printf("\r\n");
}
/* 释放图对象内存空间 */
loocLinkedGraph_delete(linkGraph);
loocLinkedGraph_delete(MST_link);
/* 报告内存泄露情况 */
looc_report();
/* 最长递增子序列 */
int X[9] = { 3, 6, 1, 4, 2, 8, 9, 5, 7 };
int LIS_Res[9];
int LIS_Path[9];
int LIS_max = LIS(X, 9, LIS_Res, LIS_Path);
printLIS(X, 9, LIS_Res, LIS_Path, LIS_max);
/* 最长公共子序列/串 */
int Y[9] = { 3, 6, 0, 2, 8, 9, 7, 1, 5 };
int** LCS_Res = (int**) looc_malloc(10 * sizeof(int*), "LCS_Res",
looc_file_line);
int** LCS_Path = (int**) looc_malloc(10 * sizeof(int*), "LCS_Path",
looc_file_line);
for (i = 0; i < 10; i++) {
LCS_Res[i] = (int*) looc_malloc(10 * sizeof(int), "LCS_Res[]",
looc_file_line);
LCS_Path[i] = (int*) looc_malloc(10 * sizeof(int), "LCS_Path[]",