diff --git a/01.LinkedLists/main.c b/01.LinkedLists/_main.c similarity index 100% rename from 01.LinkedLists/main.c rename to 01.LinkedLists/_main.c diff --git a/02.DoublyLinkedLists/DoublyLinkedList.c b/02.DoublyLinkedLists/DoublyLinkedList.c new file mode 100644 index 0000000..830ce36 --- /dev/null +++ b/02.DoublyLinkedLists/DoublyLinkedList.c @@ -0,0 +1,122 @@ +// +// DoublyLinkedList.c +// 02.DoublyLinkedLists +// +// Created by mixmex on 3/12/17. +// Copyright © 2017 mixmex. All rights reserved. +// + +#include "DoublyLinkedList.h" + +int list_length(DLLNode *head){ + DLLNode *currNode = head; + int count = 0; + + while (currNode != NULL) { + count++; + currNode = currNode->next; + } + + return count; +} + +void traversing_list(DLLNode *head){ + DLLNode *currNode = head; + int position = 1; + + if (head == NULL) { + printf("DLL is Empty\n"); + }else{ + printf("Traversing Doubly Linked List\n"); + while (currNode != NULL) { + printf("Node %d: %d \n", position, currNode->data); + position++; + currNode = currNode->next; + } + } +} + +DLLNode *insert_in_list(DLLNode *head, int data, int position){ + if (position> list_length(head) + 1) { + printf("Position> Length of List \n"); + } else { + int k = 1; + DLLNode *temp, *newNode; + newNode = (DLLNode *) malloc(sizeof(DLLNode)); + + if (newNode == NULL) { + printf("Memory error"); + }else{ + newNode->data = data; + if (position == 1) { + newNode->next = head; + newNode->prev = NULL; + if (head != NULL) { + head->prev = newNode; + } + head = newNode; + }else{ + temp = head; + while ((k < position) && (temp->next != NULL)) { + temp = temp->next; + k++; + } + if (temp->next == NULL) { + newNode->next = temp->next; + newNode->prev = temp; + temp->next = newNode; + } else { + newNode->next = temp->next; + newNode->prev = temp; + temp->next->prev = newNode; + temp->next = newNode; + } + } + } + } + return head; +} + +DLLNode *create_random_list(int number_node){ + DLLNode *head = NULL; + int i = 1; + + while (i <= number_node) { + head = insert_in_list(head, rand(), i); + i++; + } + + return head; +} + +DLLNode *delete_from_list(DLLNode *head, int position){ + DLLNode *temp1 = head, *temp2; + int k = 1; + if (head == NULL) { + printf("List is Empty \n"); + }else{ + if (position == 1) { + head = head->next; + if (head != NULL) { + head->prev = NULL; + } + free(temp1); + }else{ + while ((k < position) && (temp1->next != NULL)) { + temp1 = temp1->next; + k++; + } + if (temp1->next == NULL) { + temp2 = temp1->prev; + temp2->next = NULL; + free(temp1); + }else{ + temp2 = temp1->prev; + temp2->next = temp1->next; + temp1->next->prev = temp2; + free(temp1); + } + } + } + return head; +} diff --git a/02.DoublyLinkedLists/DoublyLinkedList.h b/02.DoublyLinkedLists/DoublyLinkedList.h new file mode 100644 index 0000000..f753837 --- /dev/null +++ b/02.DoublyLinkedLists/DoublyLinkedList.h @@ -0,0 +1,28 @@ +// +// DoublyLinkedList.h +// 02.DoublyLinkedLists +// +// Created by mixmex on 3/12/17. +// Copyright © 2017 mixmex. All rights reserved. +// + +#ifndef DoublyLinkedList_h +#define DoublyLinkedList_h + +#include +#include + +typedef struct Node{ + int data; + struct Node *next; + struct Node *prev; +} DLLNode; + +DLLNode * create_random_list(int); +int list_length(DLLNode *); +void traversing_list(DLLNode *); +DLLNode *insert_in_list(DLLNode *, int, int); +DLLNode *delete_from_list(DLLNode *, int); + + +#endif /* DoublyLinkedList_h */ diff --git a/02.DoublyLinkedLists/DoublyLinkedLists.strings b/02.DoublyLinkedLists/DoublyLinkedLists.strings new file mode 100644 index 0000000..cd6960f --- /dev/null +++ b/02.DoublyLinkedLists/DoublyLinkedLists.strings @@ -0,0 +1,19 @@ +/* + DoublyLinkedLists.strings + 02.DoublyLinkedLists + + Created by mixmex on 3/12/17. + Copyright © 2017 mixmex. All rights reserved. +*/ +ADT Doubly Linked List (Tạm dịch: Danh sách liên kết đôi) + +Advantage of a doubly linked list: + - Can navigate in both directions +Disadvantages of boubly linked list: + - Each node requires an extra pointer, requiring more space + - The insertion or deletion of a node takes a bit longer + +Basic Operation: + - Traversing the list + - Insert an item in the list + - Deleting an item form the list \ No newline at end of file diff --git a/02.DoublyLinkedLists/_main.c b/02.DoublyLinkedLists/_main.c new file mode 100644 index 0000000..678b99d --- /dev/null +++ b/02.DoublyLinkedLists/_main.c @@ -0,0 +1,24 @@ +// +// main.c +// 02.DoublyLinkedLists +// +// Created by mixmex on 3/12/17. +// Copyright © 2017 mixmex. All rights reserved. +// + +#include +#include "DoublyLinkedList.h" + + +int main(int argc, const char * argv[]) { + // insert code here... + printf("Hello, This is Demontration Program\n"); + + DLLNode *head = create_random_list(10); + traversing_list(head); + + head = delete_from_list(head, 5); + traversing_list(head); + + return 0; +} diff --git a/03.CircularLinkedLists/CircularLinkedLists.c b/03.CircularLinkedLists/CircularLinkedLists.c new file mode 100644 index 0000000..7a3ee11 --- /dev/null +++ b/03.CircularLinkedLists/CircularLinkedLists.c @@ -0,0 +1,109 @@ +// +// CircularLinkedLists.c +// 03.CircularLinkedLists +// +// Created by mixmex on 3/27/17. +// Copyright © 2017 mixmex. All rights reserved. +// + +#include "CircularLinkedLists.h" + +int circular_list_length(CLLNode *head){ + CLLNode *current = head; + int count = 0; + if (head != NULL) { + do { + current = current->next; + count++; + } while (current != head); + } + return count; +} + +void print_circular_list(CLLNode *head){ + CLLNode *current = head; + if (head != NULL) { + do { + printf("%d \t",current->data); + current = current->next; + } while (current != head); + }else{ + printf("Circular Lists is Empty\n"); + } +} + +CLLNode *insert_at_end_in_CLL(CLLNode *head, int data){ + CLLNode *current = head; + CLLNode *newNode = (CLLNode *) malloc(sizeof(CLLNode)); + if (newNode == NULL) { + printf("Memory Error\n"); + }else{ + newNode->data = data; + newNode->next = newNode; + if (head == NULL) { + head = newNode; + }else{ + while (current->next != head) { + current = current->next; + } + newNode->next = head; + current->next = newNode; + } + } + return head; +} + +CLLNode *insert_at_begin_in_CLL(CLLNode *head, int data){ + CLLNode *current = head; + CLLNode *newNode = (CLLNode *) malloc(sizeof(CLLNode)); + if (newNode == NULL) { + printf("Memory Error\n"); + }else{ + newNode->data = data; + newNode->next = newNode; + if (head == NULL) { + head = newNode; + }else{ + while (current->next != head) { + current = current->next; + } + newNode->next = head; + current->next = newNode; + head = newNode; + } + } + return head; +} + +CLLNode *delete_last_node_from_CLL(CLLNode *head){ + CLLNode *temp = head; + CLLNode *current = head; + if (head == NULL) { + printf("List is Empty\n"); + }else{ + while (current->next != head) { + temp = current; + current = current->next; + } + temp->next = head; + free(current); + } + return head; +} + +CLLNode *delete_first_node_from_CLL(CLLNode *head){ + CLLNode *temp = head; + CLLNode *current = head; + if (head == NULL) { + printf("List is Empty\n"); + }else{ + while (current->next != head) { + current = current->next; + } + current->next = head->next; + head = head->next; + free(temp); + } + return head; +} + diff --git a/03.CircularLinkedLists/CircularLinkedLists.h b/03.CircularLinkedLists/CircularLinkedLists.h new file mode 100644 index 0000000..c62bd9f --- /dev/null +++ b/03.CircularLinkedLists/CircularLinkedLists.h @@ -0,0 +1,27 @@ +// +// CircularLinkedLists.h +// 03.CircularLinkedLists +// +// Created by mixmex on 3/27/17. +// Copyright © 2017 mixmex. All rights reserved. +// + +#ifndef CircularLinkedLists_h +#define CircularLinkedLists_h + +#include +#include + +typedef struct Node{ + int data; + struct Node *next; +}CLLNode; + +int circular_list_Length(CLLNode *); +void print_circular_list(CLLNode *); +CLLNode *insert_at_end_in_CLL(CLLNode *, int); +CLLNode *insert_at_begin_in_CLL(CLLNode *, int); +CLLNode *delete_last_node_from_CLL(CLLNode *); +CLLNode *delete_first_node_from_CLL(CLLNode *); + +#endif /* CircularLinkedLists_h */ diff --git a/03.CircularLinkedLists/CircularLinkedLists.strings b/03.CircularLinkedLists/CircularLinkedLists.strings new file mode 100644 index 0000000..5b1514c --- /dev/null +++ b/03.CircularLinkedLists/CircularLinkedLists.strings @@ -0,0 +1,23 @@ +/* + CircularLinkedLists.strings + 03.CircularLinkedLists + + Created by mixmex on 3/27/17. + Copyright © 2017 mixmex. All rights reserved. +*/ + +ADT Circular Linked List (Tạm dịch: Danh sách liên kết vòng) + +Application of Circular List: +- Used in managing the computing resources of a computer. +- Implementing stacks and queues + +Basic Operation: +- Counting Nodes in a Circular List +- Printing the contents of a circular list +- Inserting a Node + * at the End + * at the Front +- Delete + * the Last Node + * the first Node \ No newline at end of file diff --git a/03.CircularLinkedLists/_main.c b/03.CircularLinkedLists/_main.c new file mode 100644 index 0000000..93e1bdc --- /dev/null +++ b/03.CircularLinkedLists/_main.c @@ -0,0 +1,34 @@ +// +// main.c +// 03.CircularLinkedLists +// +// Created by mixmex on 3/27/17. +// Copyright © 2017 mixmex. All rights reserved. +// + +#include +#include "CircularLinkedLists.h" + +int main(int argc, const char * argv[]) { + // insert code here... + printf("Hello, World! This is Demonstration Programming\n"); + + CLLNode *head; + + head = insert_at_end_in_CLL(head, 5); + head = insert_at_begin_in_CLL(head, 0); + head = insert_at_begin_in_CLL(head, 1); + head = insert_at_begin_in_CLL(head, 2); + head = insert_at_end_in_CLL(head, 3); + head = insert_at_begin_in_CLL(head, 4); + + print_circular_list(head); + + head = delete_first_node_from_CLL(head); + head = delete_last_node_from_CLL(head); + + print_circular_list(head); + + + return 0; +} diff --git a/BackTracking/Queen.c b/BackTracking/Queen.c new file mode 100644 index 0000000..2d70e00 --- /dev/null +++ b/BackTracking/Queen.c @@ -0,0 +1,45 @@ +// +// Queen.c +// DataStructure-Algorithm +// +// Created by Pham Hoan on 23/10/2021. +// + +#include +#include "Queen.h" + +int a[20], count = 0; + +void print_result(int n) { + count++; + char c = '0'; + char x = '1'; + printf("%d.\n", count); + for (int i = 1; i <= n; i++) { + for (int t = 1; t < a[i]; t++) printf(" %c ", c); + printf(" %c ", x); + for (int t = a[i] + 1; t <= n; t++) printf(" %c ", c); + printf("\n"); + } + printf("\n"); +} + +int is_candidate(int x, int y) { + for (int i = 1; i < x; i++) { + if ((y == a[i]) || (fabs(y-a[i]) == x-i)) { + return 0; + } + } + return 1; +} + +void queen(int i, int n) { // Quân Hậu ở hàng i, cột j + for (int j = 1; j <= n; j++) { + if (is_candidate(i, j)) { + a[i] = j; + if (i == n) print_result(n); + else queen(i+1, n); + } + } +} + diff --git a/BackTracking/Queen.h b/BackTracking/Queen.h new file mode 100644 index 0000000..9d233ac --- /dev/null +++ b/BackTracking/Queen.h @@ -0,0 +1,15 @@ +// +// Queen.h +// DataStructure-Algorithm +// +// Created by Pham Hoan on 23/10/2021. +// + +#ifndef Queen_h +#define Queen_h + +#include + +void queen(int i, int n); + +#endif /* Queen_h */ diff --git a/BinarySearch/BinarySearch.c b/BinarySearch/BinarySearch.c new file mode 100644 index 0000000..037b1c8 --- /dev/null +++ b/BinarySearch/BinarySearch.c @@ -0,0 +1,27 @@ +// +// BinarySearch.c +// DataStructure-Algorithm +// +// Created by Pham Hoan on 17/10/2021. +// + +#include "BinarySearch.h" + + +int binary_search(int *a, int start, int finish, int x) { + int middle = (start + finish)/2; + if (start == finish) { + if (a[middle] == x) { + return middle; + } else { + return -1; + } + } + if (a[middle] == x) { + return middle; + } else if (a[middle]> x) { + return binary_search(a, start, middle-1, x); + } else { + return binary_search(a, middle+1, finish, x); + } +} diff --git a/BinarySearch/BinarySearch.h b/BinarySearch/BinarySearch.h new file mode 100644 index 0000000..cd9c3cd --- /dev/null +++ b/BinarySearch/BinarySearch.h @@ -0,0 +1,15 @@ +// +// BinarySearch.h +// DataStructure-Algorithm +// +// Created by Pham Hoan on 17/10/2021. +// + +#ifndef BinarySearch_h +#define BinarySearch_h + +#include + +int binary_search(int *a, int start, int finish, int x); + +#endif /* BinarySearch_h */ diff --git a/DataStructure-Algorithm/main.c b/DataStructure-Algorithm/main.c new file mode 100644 index 0000000..e5ea582 --- /dev/null +++ b/DataStructure-Algorithm/main.c @@ -0,0 +1,67 @@ +// +// main.c +// DataStructure-Algorithm +// +// Created by Pham Hoan on 10/10/2021. +// + +#include +#include "../SortAlgorithm/BubbleSort.h" +#include "../BinarySearch/BinarySearch.h" +#include "../SortAlgorithm/MergeSort.h" +#include "../BackTracking/Queen.h" +#include "../Tree/LeftMostChild_RightSibling.h" + + +int main(int argc, const char * argv[]) { +// int n = 10; +// int input[10] = {3, 5, 2, 7, 1, 0, 4, 8, 9, 6}; +// +// bubble_sort(input, n); +// +// printf("kết quả: "); +// for (int i = 0; i < n; i++){ +// printf("%d ", input[i]); +// } + +// int a[10] = {0, 1, 3, 5, 7, 9, 10, 11, 15, 17}; +// int index = binary_search(a, 0, 9, 10); +// printf("Index: %d\n", index); + +// int n; +// printf("Input n = "); scanf("%d", &n); +// queen(1, n); + + treeNode * root = make_treeNode(0); + treeNode * b = make_treeNode(1); + treeNode * c = make_treeNode(2); + treeNode * d = make_treeNode(3); + treeNode * e = make_treeNode(4); + treeNode * f = make_treeNode(5); + treeNode * g = make_treeNode(6); + treeNode * h = make_treeNode(7); + treeNode * i = make_treeNode(8); + treeNode * j = make_treeNode(9); + treeNode * k = make_treeNode(10); + + root->leftmost_child = b; + b->right_sibling = c; + b->leftmost_child = e; + e->right_sibling = f; + c->leftmost_child = g; + c->right_sibling = d; + g->leftmost_child = h; + h->right_sibling = i; + i->right_sibling = j; + j->right_sibling = k; + + + printf("Pre Order\n"); + preOrder(root); + printf("In Order\n"); + inOrder(root); + printf("Post Order\n"); + postOrder(root); + + return 0; +} diff --git a/README.md b/README.md index b80b5be..b2dc69b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ # Data-Structure-and-Algorithm-with-C -Trong Repository này sẽ chứa các code trong môn học Cấu trúc dữ liệu và Giải thuật (Data Structure and Algorithm) được thể hiện bằng ngôn ngữ lập trình C. - -Để hiểu được tầm quan trọng của Thuật toán (Algorithm) là gì và tầm quan trọng của nó, có thể tham khảo thêm trong video sau: https://www.khanacademy.org/computing/computer-science/algorithms/intro-to-algorithms/v/what-are-algorithms - -Cấu trúc dữ liệu + Thuật toán = Chương trình (Algorithms + Data Structures = Programs) +Code về Cấu trúc dữ liệu và Giải thuật (Data Structure and Algorithm) được thể hiện bằng ngôn ngữ lập trình C. Để hiểu được tầm quan trọng của Thuật toán hãy tham khảo trong [video này](https://www.khanacademy.org/computing/computer-science/algorithms/intro-to-algorithms/v/what-are-algorithms) +**Cấu trúc dữ liệu + Thuật toán = Chương trình +_Data Structures + Algorithms = Programs_** diff --git a/SortAlgorithm/BubbleSort.c b/SortAlgorithm/BubbleSort.c new file mode 100644 index 0000000..fda10dd --- /dev/null +++ b/SortAlgorithm/BubbleSort.c @@ -0,0 +1,30 @@ +// +// BubbleSort.c +// DataStructure-Algorithm +// +// Created by Pham Hoan on 10/10/2021. +// + +#include "BubbleSort.h" + +void print_array(int arr[], int n); + +void bubble_sort(int arr[], int n){ + for (int i = n-1; i> 0; i--){ + for (int j = 1; j <= i; j++){ + if (arr[j-1]> arr[j]) { + int t = arr[j-1]; + arr[j-1] = arr[j]; + arr[j] = t; + } + } + print_array(arr, n); + } +} + +void print_array(int arr[], int n) { + for (int i = 0; i < n; i++){ + printf("%d ", arr[i]); + } + printf("\n"); +} diff --git a/SortAlgorithm/BubbleSort.h b/SortAlgorithm/BubbleSort.h new file mode 100644 index 0000000..af506d5 --- /dev/null +++ b/SortAlgorithm/BubbleSort.h @@ -0,0 +1,15 @@ +// +// BubbleSort.h +// DataStructure-Algorithm +// +// Created by Pham Hoan on 10/10/2021. +// + +#ifndef BubbleSort_h +#define BubbleSort_h + +#include + +void bubble_sort(int arr[], int n); + +#endif /* BubbleSort_h */ diff --git a/SortAlgorithm/MergeSort.c b/SortAlgorithm/MergeSort.c new file mode 100644 index 0000000..bedc51d --- /dev/null +++ b/SortAlgorithm/MergeSort.c @@ -0,0 +1,23 @@ +// +// MergeSort.c +// DataStructure-Algorithm +// +// Created by Pham Hoan on 23/10/2021. +// + +#include "MergeSort.h" + +void merge(int arr[], int p, int q, int r) { + int i = p; + int j = q+1; +} + + +void merge_sort(int arr[], int p, int r) { + if (p < r) { + int q = (p+r)/2; + merge_sort(arr, p, q); + merge_sort(arr, q+1, r); + merge(arr, p, q, r); + } +} diff --git a/SortAlgorithm/MergeSort.h b/SortAlgorithm/MergeSort.h new file mode 100644 index 0000000..f68d22c --- /dev/null +++ b/SortAlgorithm/MergeSort.h @@ -0,0 +1,15 @@ +// +// MergeSort.h +// DataStructure-Algorithm +// +// Created by Pham Hoan on 23/10/2021. +// + +#ifndef MergeSort_h +#define MergeSort_h + +#include + +void merge_sort(int arr[], int p, int r); + +#endif /* MergeSort_h */ diff --git a/Tree/LeftMostChild_RightSibling.c b/Tree/LeftMostChild_RightSibling.c new file mode 100644 index 0000000..bde0260 --- /dev/null +++ b/Tree/LeftMostChild_RightSibling.c @@ -0,0 +1,56 @@ +// +// LeftMostChild_RightSibling.c +// DataStructure-Algorithm +// +// Created by Pham Hoan on 09/12/2021. +// + +#include +#include "LeftMostChild_RightSibling.h" + +treeNode * make_treeNode(int d){ + treeNode * newNode = NULL; + newNode = (treeNode*) malloc(sizeof(treeNode)); + newNode->data = d; + newNode->leftmost_child = NULL; + newNode->right_sibling = NULL; + return newNode; +} + +void preOrder(treeNode * nodeR){ + printf("%d\n", nodeR->data); + if (nodeR->leftmost_child != NULL) { + preOrder(nodeR->leftmost_child); + treeNode * temp = nodeR->leftmost_child->right_sibling; + while (temp != NULL){ + preOrder(temp); + temp = temp->right_sibling; + } + } +} + +void inOrder(treeNode * nodeR){ + if (nodeR->leftmost_child == NULL) { + printf("%d\n", nodeR->data); + } else { + inOrder(nodeR->leftmost_child); + printf("%d\n", nodeR->data); + treeNode * temp = nodeR->leftmost_child->right_sibling; + while (temp != NULL){ + inOrder(temp); + temp = temp->right_sibling; + } + } +} + +void postOrder(treeNode * nodeR){ + if (nodeR->leftmost_child != NULL) { + postOrder(nodeR->leftmost_child); + treeNode * temp = nodeR->leftmost_child->right_sibling; + while (temp != NULL){ + postOrder(temp); + temp = temp->right_sibling; + } + } + printf("%d\n", nodeR->data); +} diff --git a/Tree/LeftMostChild_RightSibling.h b/Tree/LeftMostChild_RightSibling.h new file mode 100644 index 0000000..ed98642 --- /dev/null +++ b/Tree/LeftMostChild_RightSibling.h @@ -0,0 +1,26 @@ +// +// LeftMostChild_RightSibling.h +// DataStructure-Algorithm +// +// Created by Pham Hoan on 09/12/2021. +// + +#ifndef LeftMostChild_RightSibling_h +#define LeftMostChild_RightSibling_h + +#include + +struct Node +{ + int data; + struct Node * leftmost_child; + struct Node * right_sibling; +}; +typedef struct Node treeNode; + +treeNode * make_treeNode(int); +void preOrder(treeNode *); +void inOrder(treeNode *); +void postOrder(treeNode *); + +#endif /* LeftMostChild_RightSibling_h */

AltStyle によって変換されたページ (->オリジナル) /