-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Data Structures and Algorithms (English)
- Loading branch information
Showing
9 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
Data Structures and Algorithms (English)/6-14_Count Connected Components (20).c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
typedef enum {false, true} bool; | ||
#define MaxVertexNum 10 /* maximum number of vertices */ | ||
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */ | ||
|
||
typedef struct AdjVNode *PtrToAdjVNode; | ||
struct AdjVNode{ | ||
Vertex AdjV; | ||
PtrToAdjVNode Next; | ||
}; | ||
|
||
typedef struct Vnode{ | ||
PtrToAdjVNode FirstEdge; | ||
} AdjList[MaxVertexNum]; | ||
|
||
typedef struct GNode *PtrToGNode; | ||
struct GNode{ | ||
int Nv; | ||
int Ne; | ||
AdjList G; | ||
}; | ||
typedef PtrToGNode LGraph; | ||
|
||
int CountConnectedComponents( LGraph Graph ) { | ||
int count = 0; | ||
bool visited[MaxVertexNum]; | ||
int queue[MaxVertexNum]; | ||
int front = 0, rear = 0; | ||
for (int i = 0; i < Graph->Nv; i++) visited[i] = false; | ||
for (int i = 0; i < Graph->Nv; i++) { | ||
if (visited[i] == true) continue; | ||
count++; | ||
visited[i] = true; | ||
queue[rear++] = i; | ||
while (front != rear) { | ||
int v = queue[front++]; | ||
PtrToAdjVNode p = Graph->G[v].FirstEdge; | ||
while (p) { | ||
if (visited[p->AdjV] == false) { | ||
visited[p->AdjV] = true; | ||
queue[rear++] = p->AdjV; | ||
} | ||
p = p->Next; | ||
} | ||
} | ||
} | ||
return count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
Deque CreateDeque() { | ||
Deque dq = (Deque)malloc(sizeof(struct DequeRecord)); | ||
PtrToNode sentinel = (PtrToNode)malloc(sizeof(struct Node)); | ||
|
||
dq -> Front = sentinel; | ||
dq -> Rear = sentinel; | ||
|
||
sentinel -> Last = NULL; | ||
sentinel -> Next = NULL; | ||
|
||
return dq; | ||
} | ||
|
||
int Push( ElementType X, Deque D ) { | ||
PtrToNode node = (PtrToNode)malloc(sizeof(struct Node)); | ||
node -> Element = X; | ||
|
||
PtrToNode sentinel = D -> Front; | ||
|
||
node -> Last = sentinel; | ||
node -> Next = sentinel -> Next; | ||
|
||
if (D -> Front == D -> Rear) { | ||
sentinel -> Next = node; | ||
D -> Rear = node; | ||
} else { | ||
sentinel -> Next -> Last = node; | ||
sentinel -> Next = node; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
ElementType Pop( Deque D ) { | ||
if (D -> Front == D -> Rear) return ERROR; | ||
|
||
PtrToNode node = D -> Front -> Next; | ||
ElementType result = node -> Element; | ||
|
||
if (node -> Next == NULL) { | ||
D -> Front -> Next = NULL; | ||
D -> Rear = D -> Front; | ||
} else { | ||
node -> Next -> Last = D -> Front; | ||
D -> Front -> Next = node -> Next; | ||
} | ||
|
||
free(node); | ||
|
||
return result; | ||
} | ||
|
||
int Inject( ElementType X, Deque D ) { | ||
PtrToNode node = (PtrToNode)malloc(sizeof(struct Node)); | ||
|
||
node -> Element = X; | ||
D -> Rear -> Next = node; | ||
node -> Last = D -> Rear; | ||
D -> Rear = node; | ||
|
||
return 1; | ||
} | ||
|
||
ElementType Eject( Deque D ) { | ||
if (D -> Front == D -> Rear) return ERROR; | ||
|
||
PtrToNode node = D -> Rear; | ||
ElementType result = node -> Element; | ||
|
||
node -> Last -> Next = NULL; | ||
D -> Rear = node -> Last; | ||
|
||
free(node); | ||
|
||
return result; | ||
} |
32 changes: 32 additions & 0 deletions
32
Data Structures and Algorithms (English)/6-2_Two Stacks In One Array (20).c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Stack CreateStack( int MaxElements ) { | ||
Stack stack = (Stack)malloc(sizeof(struct StackRecord)); | ||
stack->Array = (ElementType *)malloc(sizeof(ElementType) * MaxElements); | ||
stack->Top1 = -1; | ||
stack->Top2 = MaxElements; | ||
stack->Capacity = MaxElements; | ||
return stack; | ||
} | ||
|
||
int IsEmpty( Stack S, int Stacknum ) { | ||
if (Stacknum == 1) return S->Top1 == -1; | ||
else if (Stacknum == 2) return S->Top2 == S->Capacity; | ||
} | ||
|
||
int IsFull( Stack S ) { | ||
if (S->Top1 + 1 == S->Top2) return 1; | ||
else return 0; | ||
} | ||
|
||
int Push( ElementType X, Stack S, int Stacknum ) { | ||
if (IsFull(S)) return 0; | ||
if (Stacknum == 1) S->Array[++S->Top1] = X; | ||
else if (Stacknum == 2) S->Array[--S->Top2] = X; | ||
return 1; | ||
|
||
} | ||
|
||
ElementType Top_Pop( Stack S, int Stacknum ) { | ||
if (IsEmpty(S, Stacknum)) return ERROR; | ||
if (Stacknum == 1) return S->Array[S->Top1--]; | ||
else if (Stacknum == 2) return S->Array[S->Top2++]; | ||
} |
29 changes: 29 additions & 0 deletions
29
Data Structures and Algorithms (English)/6-3_Add Two Polynomials (20).c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Polynomial Add( Polynomial a, Polynomial b ) { | ||
Polynomial head, cur; | ||
cur = head = a; | ||
a = a->Next; | ||
b = b->Next; | ||
while (a && b) { | ||
if (a->Exponent > b->Exponent) { | ||
cur->Next = a; | ||
a = a->Next; | ||
cur = cur->Next; | ||
} else if (a->Exponent < b->Exponent) { | ||
cur->Next = b; | ||
b = b->Next; | ||
cur = cur->Next; | ||
} else { | ||
if (a->Coefficient + b->Coefficient != 0) { | ||
cur->Next = a; | ||
a->Coefficient += b->Coefficient; | ||
cur = cur->Next; | ||
} | ||
a = a->Next; | ||
b = b->Next; | ||
} | ||
} | ||
if (a) cur->Next = a; | ||
else cur->Next = b; | ||
|
||
return head; | ||
} |
15 changes: 15 additions & 0 deletions
15
Data Structures and Algorithms (English)/6-4_Reverse Linked List (20).c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
List Reverse( List L ) { | ||
if (!L || !L->Next) return L; | ||
Position p, q, r; | ||
p = L->Next; | ||
q = p->Next; | ||
p->Next = NULL; | ||
while (q) { | ||
r = q->Next; | ||
q->Next = p; | ||
p = q; | ||
q = r; | ||
} | ||
L->Next = p; | ||
return L; | ||
} |
8 changes: 8 additions & 0 deletions
8
Data Structures and Algorithms (English)/6-7_Isomorphic (20).c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
int Isomorphic( Tree T1, Tree T2 ) { | ||
return | ||
(T1 == NULL && T2 == NULL) || ( | ||
T1 != NULL && T2 != NULL && | ||
T1->Element == T2->Element && | ||
((Isomorphic(T1->Left, T2->Left) && Isomorphic(T1->Right, T2->Right)) | ||
|| (Isomorphic(T1->Left, T2->Right) && Isomorphic(T1->Right, T2->Left)))); | ||
} |
56 changes: 56 additions & 0 deletions
56
Data Structures and Algorithms (English)/6-8_Percolate Up and Down (20).c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
typedef int ElementType; | ||
#define MinData -1 | ||
|
||
typedef struct HeapStruct *PriorityQueue; | ||
struct HeapStruct { | ||
ElementType *Elements; | ||
int Capacity; | ||
int Size; | ||
}; | ||
|
||
PriorityQueue Initialize( int MaxElements ); /* details omitted */ | ||
|
||
void PercolateUp( int p, PriorityQueue H ); | ||
void PercolateDown( int p, PriorityQueue H ); | ||
|
||
void Insert( ElementType X, PriorityQueue H ) | ||
{ | ||
int p = ++H->Size; | ||
H->Elements[p] = X; | ||
PercolateUp( p, H ); | ||
} | ||
|
||
ElementType DeleteMin( PriorityQueue H ) | ||
{ | ||
ElementType MinElement; | ||
MinElement = H->Elements[1]; | ||
H->Elements[1] = H->Elements[H->Size--]; | ||
PercolateDown( 1, H ); | ||
return MinElement; | ||
} | ||
|
||
void PercolateUp( int p, PriorityQueue H ) { | ||
while (p > 1) { | ||
int parent = p / 2; | ||
if (H->Elements[p] < H->Elements[parent]) { | ||
ElementType tmp = H->Elements[p]; | ||
H->Elements[p] = H->Elements[parent]; | ||
H->Elements[parent] = tmp; | ||
p = parent; | ||
} else break; | ||
} | ||
} | ||
|
||
void PercolateDown( int p, PriorityQueue H ) { | ||
while (p * 2 <= H->Size) { | ||
int child = p * 2; | ||
if (child + 1 <= H->Size && H->Elements[child + 1] < H->Elements[child]) child++; | ||
if (H->Elements[p] > H->Elements[child]) { | ||
ElementType tmp = H->Elements[p]; | ||
H->Elements[p] = H->Elements[child]; | ||
H->Elements[child] = tmp; | ||
p = child; | ||
} else break; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Data Structures and Algorithms (English)/6-9_Sort Three Distinct Keys (20).c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
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. | ||
*/ | ||
void MySort( ElementType A[], int N ) { | ||
int falseCount = 0, maybeCount = 0, trueCount = 0; | ||
for (int i = 0; i < N; i++) { | ||
if (A[i] == false) falseCount++; | ||
else if (A[i] == maybe) maybeCount++; | ||
else trueCount++; | ||
} | ||
for (int i = 0; i < falseCount; i++) A[i] = false; | ||
for (int i = falseCount; i < falseCount + maybeCount; i++) A[i] = maybe; | ||
for (int i = falseCount + maybeCount; i < N; i++) A[i] = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters