From f8291c9335afc971ca8be43dd1e9cb49f6a4d067 Mon Sep 17 00:00:00 2001 From: tiny656 Date: Wed, 10 Jan 2024 21:29:43 +0800 Subject: [PATCH] add Data Structures and Algorithms (English) --- .../6-14_Count Connected Components (20).c | 47 ++++++++++++ .../6-1_Deque (25).c | 76 +++++++++++++++++++ .../6-2_Two Stacks In One Array (20).c | 32 ++++++++ .../6-3_Add Two Polynomials (20).c | 29 +++++++ .../6-4_Reverse Linked List (20).c | 15 ++++ .../6-7_Isomorphic (20).c | 8 ++ .../6-8_Percolate Up and Down (20).c | 56 ++++++++++++++ .../6-9_Sort Three Distinct Keys (20).c | 14 ++++ GenerateREADME.py | 6 ++ 9 files changed, 283 insertions(+) create mode 100644 Data Structures and Algorithms (English)/6-14_Count Connected Components (20).c create mode 100644 Data Structures and Algorithms (English)/6-1_Deque (25).c create mode 100644 Data Structures and Algorithms (English)/6-2_Two Stacks In One Array (20).c create mode 100644 Data Structures and Algorithms (English)/6-3_Add Two Polynomials (20).c create mode 100644 Data Structures and Algorithms (English)/6-4_Reverse Linked List (20).c create mode 100644 Data Structures and Algorithms (English)/6-7_Isomorphic (20).c create mode 100644 Data Structures and Algorithms (English)/6-8_Percolate Up and Down (20).c create mode 100644 Data Structures and Algorithms (English)/6-9_Sort Three Distinct Keys (20).c diff --git a/Data Structures and Algorithms (English)/6-14_Count Connected Components (20).c b/Data Structures and Algorithms (English)/6-14_Count Connected Components (20).c new file mode 100644 index 0000000..e859b0c --- /dev/null +++ b/Data Structures and Algorithms (English)/6-14_Count Connected Components (20).c @@ -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; +} \ No newline at end of file diff --git a/Data Structures and Algorithms (English)/6-1_Deque (25).c b/Data Structures and Algorithms (English)/6-1_Deque (25).c new file mode 100644 index 0000000..bc9eb21 --- /dev/null +++ b/Data Structures and Algorithms (English)/6-1_Deque (25).c @@ -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; +} \ No newline at end of file diff --git a/Data Structures and Algorithms (English)/6-2_Two Stacks In One Array (20).c b/Data Structures and Algorithms (English)/6-2_Two Stacks In One Array (20).c new file mode 100644 index 0000000..322a4d6 --- /dev/null +++ b/Data Structures and Algorithms (English)/6-2_Two Stacks In One Array (20).c @@ -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++]; +} \ No newline at end of file diff --git a/Data Structures and Algorithms (English)/6-3_Add Two Polynomials (20).c b/Data Structures and Algorithms (English)/6-3_Add Two Polynomials (20).c new file mode 100644 index 0000000..6ff9bcf --- /dev/null +++ b/Data Structures and Algorithms (English)/6-3_Add Two Polynomials (20).c @@ -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; +} \ No newline at end of file diff --git a/Data Structures and Algorithms (English)/6-4_Reverse Linked List (20).c b/Data Structures and Algorithms (English)/6-4_Reverse Linked List (20).c new file mode 100644 index 0000000..37dcff9 --- /dev/null +++ b/Data Structures and Algorithms (English)/6-4_Reverse Linked List (20).c @@ -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; +} \ No newline at end of file diff --git a/Data Structures and Algorithms (English)/6-7_Isomorphic (20).c b/Data Structures and Algorithms (English)/6-7_Isomorphic (20).c new file mode 100644 index 0000000..48ac515 --- /dev/null +++ b/Data Structures and Algorithms (English)/6-7_Isomorphic (20).c @@ -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)))); +} \ No newline at end of file diff --git a/Data Structures and Algorithms (English)/6-8_Percolate Up and Down (20).c b/Data Structures and Algorithms (English)/6-8_Percolate Up and Down (20).c new file mode 100644 index 0000000..2d17f96 --- /dev/null +++ b/Data Structures and Algorithms (English)/6-8_Percolate Up and Down (20).c @@ -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; + } +} \ No newline at end of file diff --git a/Data Structures and Algorithms (English)/6-9_Sort Three Distinct Keys (20).c b/Data Structures and Algorithms (English)/6-9_Sort Three Distinct Keys (20).c new file mode 100644 index 0000000..337692a --- /dev/null +++ b/Data Structures and Algorithms (English)/6-9_Sort Three Distinct Keys (20).c @@ -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; +} \ No newline at end of file diff --git a/GenerateREADME.py b/GenerateREADME.py index a818b5f..a23756a 100644 --- a/GenerateREADME.py +++ b/GenerateREADME.py @@ -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'团体程序设计天梯赛-练习集', @@ -59,6 +64,7 @@ - [中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题](#中国大学mooc-陈越何钦铭-数据结构-起步能力自测题) - [基础编程题目集](#基础编程题目集) - [数据结构与算法题目集(中文)](#数据结构与算法题目集中文) +- [Data Structures and Algorithms (English)](#data-structures-and-algorithms-english) - [团体程序设计天梯赛-练习集](#团体程序设计天梯赛-练习集) '''