Skip to content

Commit f8291c9

Browse files
committed
add Data Structures and Algorithms (English)
1 parent f7eded7 commit f8291c9

9 files changed

+283
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
typedef enum {false, true} bool;
2+
#define MaxVertexNum 10 /* maximum number of vertices */
3+
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */
4+
5+
typedef struct AdjVNode *PtrToAdjVNode;
6+
struct AdjVNode{
7+
Vertex AdjV;
8+
PtrToAdjVNode Next;
9+
};
10+
11+
typedef struct Vnode{
12+
PtrToAdjVNode FirstEdge;
13+
} AdjList[MaxVertexNum];
14+
15+
typedef struct GNode *PtrToGNode;
16+
struct GNode{
17+
int Nv;
18+
int Ne;
19+
AdjList G;
20+
};
21+
typedef PtrToGNode LGraph;
22+
23+
int CountConnectedComponents( LGraph Graph ) {
24+
int count = 0;
25+
bool visited[MaxVertexNum];
26+
int queue[MaxVertexNum];
27+
int front = 0, rear = 0;
28+
for (int i = 0; i < Graph->Nv; i++) visited[i] = false;
29+
for (int i = 0; i < Graph->Nv; i++) {
30+
if (visited[i] == true) continue;
31+
count++;
32+
visited[i] = true;
33+
queue[rear++] = i;
34+
while (front != rear) {
35+
int v = queue[front++];
36+
PtrToAdjVNode p = Graph->G[v].FirstEdge;
37+
while (p) {
38+
if (visited[p->AdjV] == false) {
39+
visited[p->AdjV] = true;
40+
queue[rear++] = p->AdjV;
41+
}
42+
p = p->Next;
43+
}
44+
}
45+
}
46+
return count;
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Deque CreateDeque() {
2+
Deque dq = (Deque)malloc(sizeof(struct DequeRecord));
3+
PtrToNode sentinel = (PtrToNode)malloc(sizeof(struct Node));
4+
5+
dq -> Front = sentinel;
6+
dq -> Rear = sentinel;
7+
8+
sentinel -> Last = NULL;
9+
sentinel -> Next = NULL;
10+
11+
return dq;
12+
}
13+
14+
int Push( ElementType X, Deque D ) {
15+
PtrToNode node = (PtrToNode)malloc(sizeof(struct Node));
16+
node -> Element = X;
17+
18+
PtrToNode sentinel = D -> Front;
19+
20+
node -> Last = sentinel;
21+
node -> Next = sentinel -> Next;
22+
23+
if (D -> Front == D -> Rear) {
24+
sentinel -> Next = node;
25+
D -> Rear = node;
26+
} else {
27+
sentinel -> Next -> Last = node;
28+
sentinel -> Next = node;
29+
}
30+
31+
return 1;
32+
}
33+
34+
ElementType Pop( Deque D ) {
35+
if (D -> Front == D -> Rear) return ERROR;
36+
37+
PtrToNode node = D -> Front -> Next;
38+
ElementType result = node -> Element;
39+
40+
if (node -> Next == NULL) {
41+
D -> Front -> Next = NULL;
42+
D -> Rear = D -> Front;
43+
} else {
44+
node -> Next -> Last = D -> Front;
45+
D -> Front -> Next = node -> Next;
46+
}
47+
48+
free(node);
49+
50+
return result;
51+
}
52+
53+
int Inject( ElementType X, Deque D ) {
54+
PtrToNode node = (PtrToNode)malloc(sizeof(struct Node));
55+
56+
node -> Element = X;
57+
D -> Rear -> Next = node;
58+
node -> Last = D -> Rear;
59+
D -> Rear = node;
60+
61+
return 1;
62+
}
63+
64+
ElementType Eject( Deque D ) {
65+
if (D -> Front == D -> Rear) return ERROR;
66+
67+
PtrToNode node = D -> Rear;
68+
ElementType result = node -> Element;
69+
70+
node -> Last -> Next = NULL;
71+
D -> Rear = node -> Last;
72+
73+
free(node);
74+
75+
return result;
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Stack CreateStack( int MaxElements ) {
2+
Stack stack = (Stack)malloc(sizeof(struct StackRecord));
3+
stack->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElements);
4+
stack->Top1 = -1;
5+
stack->Top2 = MaxElements;
6+
stack->Capacity = MaxElements;
7+
return stack;
8+
}
9+
10+
int IsEmpty( Stack S, int Stacknum ) {
11+
if (Stacknum == 1) return S->Top1 == -1;
12+
else if (Stacknum == 2) return S->Top2 == S->Capacity;
13+
}
14+
15+
int IsFull( Stack S ) {
16+
if (S->Top1 + 1 == S->Top2) return 1;
17+
else return 0;
18+
}
19+
20+
int Push( ElementType X, Stack S, int Stacknum ) {
21+
if (IsFull(S)) return 0;
22+
if (Stacknum == 1) S->Array[++S->Top1] = X;
23+
else if (Stacknum == 2) S->Array[--S->Top2] = X;
24+
return 1;
25+
26+
}
27+
28+
ElementType Top_Pop( Stack S, int Stacknum ) {
29+
if (IsEmpty(S, Stacknum)) return ERROR;
30+
if (Stacknum == 1) return S->Array[S->Top1--];
31+
else if (Stacknum == 2) return S->Array[S->Top2++];
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Polynomial Add( Polynomial a, Polynomial b ) {
2+
Polynomial head, cur;
3+
cur = head = a;
4+
a = a->Next;
5+
b = b->Next;
6+
while (a && b) {
7+
if (a->Exponent > b->Exponent) {
8+
cur->Next = a;
9+
a = a->Next;
10+
cur = cur->Next;
11+
} else if (a->Exponent < b->Exponent) {
12+
cur->Next = b;
13+
b = b->Next;
14+
cur = cur->Next;
15+
} else {
16+
if (a->Coefficient + b->Coefficient != 0) {
17+
cur->Next = a;
18+
a->Coefficient += b->Coefficient;
19+
cur = cur->Next;
20+
}
21+
a = a->Next;
22+
b = b->Next;
23+
}
24+
}
25+
if (a) cur->Next = a;
26+
else cur->Next = b;
27+
28+
return head;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
List Reverse( List L ) {
2+
if (!L || !L->Next) return L;
3+
Position p, q, r;
4+
p = L->Next;
5+
q = p->Next;
6+
p->Next = NULL;
7+
while (q) {
8+
r = q->Next;
9+
q->Next = p;
10+
p = q;
11+
q = r;
12+
}
13+
L->Next = p;
14+
return L;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int Isomorphic( Tree T1, Tree T2 ) {
2+
return
3+
(T1 == NULL && T2 == NULL) || (
4+
T1 != NULL && T2 != NULL &&
5+
T1->Element == T2->Element &&
6+
((Isomorphic(T1->Left, T2->Left) && Isomorphic(T1->Right, T2->Right))
7+
|| (Isomorphic(T1->Left, T2->Right) && Isomorphic(T1->Right, T2->Left))));
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
typedef int ElementType;
3+
#define MinData -1
4+
5+
typedef struct HeapStruct *PriorityQueue;
6+
struct HeapStruct {
7+
ElementType *Elements;
8+
int Capacity;
9+
int Size;
10+
};
11+
12+
PriorityQueue Initialize( int MaxElements ); /* details omitted */
13+
14+
void PercolateUp( int p, PriorityQueue H );
15+
void PercolateDown( int p, PriorityQueue H );
16+
17+
void Insert( ElementType X, PriorityQueue H )
18+
{
19+
int p = ++H->Size;
20+
H->Elements[p] = X;
21+
PercolateUp( p, H );
22+
}
23+
24+
ElementType DeleteMin( PriorityQueue H )
25+
{
26+
ElementType MinElement;
27+
MinElement = H->Elements[1];
28+
H->Elements[1] = H->Elements[H->Size--];
29+
PercolateDown( 1, H );
30+
return MinElement;
31+
}
32+
33+
void PercolateUp( int p, PriorityQueue H ) {
34+
while (p > 1) {
35+
int parent = p / 2;
36+
if (H->Elements[p] < H->Elements[parent]) {
37+
ElementType tmp = H->Elements[p];
38+
H->Elements[p] = H->Elements[parent];
39+
H->Elements[parent] = tmp;
40+
p = parent;
41+
} else break;
42+
}
43+
}
44+
45+
void PercolateDown( int p, PriorityQueue H ) {
46+
while (p * 2 <= H->Size) {
47+
int child = p * 2;
48+
if (child + 1 <= H->Size && H->Elements[child + 1] < H->Elements[child]) child++;
49+
if (H->Elements[p] > H->Elements[child]) {
50+
ElementType tmp = H->Elements[p];
51+
H->Elements[p] = H->Elements[child];
52+
H->Elements[child] = tmp;
53+
p = child;
54+
} else break;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Suppose you have an array of N elements, containing three distinct keys, "true", "false", and "maybe". Given an O(N) algorithm to rearrange the list so that all "false" elements precede "maybe" elements, which in turn precede "true" elements. You may use only constant extra space.
3+
*/
4+
void MySort( ElementType A[], int N ) {
5+
int falseCount = 0, maybeCount = 0, trueCount = 0;
6+
for (int i = 0; i < N; i++) {
7+
if (A[i] == false) falseCount++;
8+
else if (A[i] == maybe) maybeCount++;
9+
else trueCount++;
10+
}
11+
for (int i = 0; i < falseCount; i++) A[i] = false;
12+
for (int i = falseCount; i < falseCount + maybeCount; i++) A[i] = maybe;
13+
for (int i = falseCount + maybeCount; i < N; i++) A[i] = true;
14+
}

GenerateREADME.py

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
'folderName': u'数据结构与算法题目集(中文)',
3434
'url': '',
3535
},
36+
{
37+
'name': u'Data Structures and Algorithms (English)',
38+
'folderName': u'Data Structures and Algorithms (English)',
39+
'url': '',
40+
},
3641
{
3742
'name': u'团体程序设计天梯赛-练习集',
3843
'folderName': u'团体程序设计天梯赛-练习集',
@@ -59,6 +64,7 @@
5964
- [中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题](#中国大学mooc-陈越何钦铭-数据结构-起步能力自测题)
6065
- [基础编程题目集](#基础编程题目集)
6166
- [数据结构与算法题目集(中文)](#数据结构与算法题目集中文)
67+
- [Data Structures and Algorithms (English)](#data-structures-and-algorithms-english)
6268
- [团体程序设计天梯赛-练习集](#团体程序设计天梯赛-练习集)
6369
6470
'''

0 commit comments

Comments
 (0)