-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
208 lines (164 loc) · 4.11 KB
/
main.cpp
File metadata and controls
208 lines (164 loc) · 4.11 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
#include "common.h"
//#include"LinearList/LinearList.h"
#include "quiz/prime.h"
#include "search/search.h"
#include "graph/graph.h"
#include "tree/BinaryTree.h"
#include "sort/sort.h"
#include "list.h"
#include "linked_list/Singly/OneLink.h"
int factorial(int n) {
if(n==1) return 1;
return factorial(n-1)*n;
}
////////////////////////////////////////////////////////////
void permutation(char *s, int start, int end) {
if(start == end) {
printf("%s\n", s);
return;
}
for(int i = start; i < end; i++) {
std::swap(s[start],s[i]);
permutation(s, start + 1, end);
std::swap(s[start],s[i]);
}
}
void testPermutation() {
char a[]="abcd";
permutation(a, 0, strlen(a));
}
////////////////////////////////////////////////////////////
/**
* if has loop return 1 else return 0
*/
int hasLoop(Node *node) {
Node *pFast=node;
Node *pSlow=node;
if(pFast == NULL || pFast->next == NULL)
return 0;
do {
pFast = pFast->next->next;
pSlow = pSlow->next;
// if(pFast != NULL) {
// printf("fast=%d ",pFast->element);
// }
// if(pSlow != NULL) {
// printf("slow=%d",pSlow->element);
// }
// printf("\n");
} while(pFast != pSlow &&pFast != NULL && pFast->next != NULL) ;
return (pFast == pSlow);
}
void testLoop() {
Node *pList=NULL;
// int length = 0;
int i;
// int posElem;
initList(&pList);
printList(pList);
// insertHeadList(&pList,5);
// printList(pList);
// insertLastList(&pList,10);
// printList(pList);
// pList=creatList(pList);
// printList(pList);
for(i=1; i<=10; ++i)
insertLastList(&pList,i);
sizeList(pList);
printList(pList);
Node* n10=getNode(pList,10);
Node* n6=getNode(pList,6);
n10->next=n6;
// int e1=getElement(pList,1);
// printf("%d\n",e1);
int flag=hasLoop(pList);
printf("%d\n",flag);
}
////////////////////////////////////////////////////////////
void hanoi(int n,char A,char B,char C) {
if(n==1) {
printf("Move disk %d from %c to %c\n",n,A,C);
} else {
hanoi(n-1,A,C,B);
printf("Move disk %d from %c to %c\n",n,A,C);
hanoi(n-1,B,A,C);
}
}
void hanoi2(int n,char a,char b,char c) {
static int step=0;
if(n==1) printf("Step %d: Move disk %d from %c to %c\n",++step,n,a,c);
else {
hanoi2(n-1,a,c,b);
hanoi2(1,a,b,c);
hanoi2(n-1,b,a,c);
}
}
int testOneLink() {
OneLink link1(8);
link1.set(2,'9');
link1.output();
return 0;
}
extern void testTree();
extern void testMaxContinuousSeqSum();
extern void testBitCount();
extern void testGCD();
extern void testLCM();
extern void testStringAlg();
namespace simplegraph {
extern void testDFSGraph();
}
namespace graphmatrix {
extern void testDFS();
extern void testPrim();
extern void testDijkstra();
}
extern void testKMax();
namespace UnionFind {
extern void testUnionFind();
}
int main() {
UnionFind::testUnionFind();
testKMax();
graphmatrix::testDijkstra();
graphmatrix::testPrim();
graphmatrix::testDFS();
simplegraph::testDFSGraph();
testStringAlg();
testLCM();
testGCD();
testBitCount();
testMaxContinuousSeqSum();
testTree();
testSortMain();
// testOneLink();
// Cutedge::testCutedge();
// Cutpoint2::testCutpoint();
// Cutpoint::testCutpoint();
// testList();
// testLoop();
// hanoi2(3,'a','b','c');
// hanoi('a','b','c',3);
// testHeapSort();
// createHeap1();
// createHeap2();
//CommonBinaryTree::test1();
//testPrime();
// shortestPathByBellmanFoardCheckingNegativeCircle() ;
// shortestPathByBellmanFoard();
// shortestPathByDijkstra();
// shortestPathByBellmanFoardUsingQueue();
// shortestPathByDijkstraLinkedGraph();
// testLinkGraph();
// shortestPathByFloydWarshall();
// ShortestPathByDFS::shortestPathByDFS();
// bfsGraph();
// dfsGraph();
// mazebyBFS();
// mazebyDFS();
// permutationByDFS();
// testFibonacci();
// LinearList::test();
// LinkedList::WithHeadNode::test();
return 0;
}