Skip to content

Commit

Permalink
add Data Structures and Algorithms (English)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny656 committed Jan 10, 2024
1 parent f7eded7 commit f8291c9
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 0 deletions.
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;
}
76 changes: 76 additions & 0 deletions Data Structures and Algorithms (English)/6-1_Deque (25).c
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;
}
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++];
}
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;
}
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;
}
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))));
}
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;
}
}
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;
}
6 changes: 6 additions & 0 deletions GenerateREADME.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
'folderName': u'数据结构与算法题目集(中文)',
'url': '',
},
{
'name': u'Data Structures and Algorithms (English)',
'folderName': u'Data Structures and Algorithms (English)',
'url': '',
},
{
'name': u'团体程序设计天梯赛-练习集',
'folderName': u'团体程序设计天梯赛-练习集',
Expand All @@ -59,6 +64,7 @@
- [中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题](#中国大学mooc-陈越何钦铭-数据结构-起步能力自测题)
- [基础编程题目集](#基础编程题目集)
- [数据结构与算法题目集(中文)](#数据结构与算法题目集中文)
- [Data Structures and Algorithms (English)](#data-structures-and-algorithms-english)
- [团体程序设计天梯赛-练习集](#团体程序设计天梯赛-练习集)
'''
Expand Down

0 comments on commit f8291c9

Please sign in to comment.