From af0f92c18db8a0dca65c7ba0dd2e7001676097dc Mon Sep 17 00:00:00 2001 From: krahets Date: Mon, 17 Apr 2023 22:15:06 +0800 Subject: [PATCH] Fine tune the C codes. --- .../c/chapter_array_and_linkedlist/my_list.c | 2 +- codes/c/chapter_heap/my_heap.c | 4 +- codes/c/chapter_searching/hashing_search.c | 1 + codes/c/chapter_searching/leetcode_two_sum.c | 3 +- codes/c/chapter_sorting/CMakeLists.txt | 2 - codes/c/chapter_sorting/bucket_sort.c | 34 ---------------- codes/c/chapter_stack_and_queue/array_queue.c | 1 + .../linkedlist_deque.c | 4 +- .../linkedlist_queue.c | 1 + .../linkedlist_stack.c | 3 +- codes/c/chapter_tree/avl_tree.c | 5 ++- codes/c/chapter_tree/binary_search_tree.c | 2 +- codes/c/include/list_node.h | 15 +------ codes/c/include/print_util.h | 39 +++---------------- codes/c/include/tree_node.h | 16 +------- 15 files changed, 29 insertions(+), 103 deletions(-) delete mode 100644 codes/c/chapter_sorting/bucket_sort.c diff --git a/codes/c/chapter_array_and_linkedlist/my_list.c b/codes/c/chapter_array_and_linkedlist/my_list.c index e2e2e703..33ec2bf3 100644 --- a/codes/c/chapter_array_and_linkedlist/my_list.c +++ b/codes/c/chapter_array_and_linkedlist/my_list.c @@ -160,4 +160,4 @@ int main() { delMyList(list); return 0; -} \ No newline at end of file +} diff --git a/codes/c/chapter_heap/my_heap.c b/codes/c/chapter_heap/my_heap.c index 5c60a8cd..29dc640a 100644 --- a/codes/c/chapter_heap/my_heap.c +++ b/codes/c/chapter_heap/my_heap.c @@ -75,7 +75,7 @@ void push(maxHeap *h, int val) { // 默认情况下,不应该添加这么多节点 if (h->size == MAX_SIZE) { printf("heap is full!"); - return NIL; + return; } // 添加节点 h->data[h->size] = val; @@ -174,4 +174,6 @@ int main() { // 释放内存 free(heap); + + return 0; } diff --git a/codes/c/chapter_searching/hashing_search.c b/codes/c/chapter_searching/hashing_search.c index 7c37ef4a..a47438ed 100644 --- a/codes/c/chapter_searching/hashing_search.c +++ b/codes/c/chapter_searching/hashing_search.c @@ -142,5 +142,6 @@ int main() { if (node) printf("目标节点值 3 的对应节点对象为 %#p val: %d\r\n", node, node->val); deleteAll(ht1); + return 0; } diff --git a/codes/c/chapter_searching/leetcode_two_sum.c b/codes/c/chapter_searching/leetcode_two_sum.c index e604ecd5..a6298199 100644 --- a/codes/c/chapter_searching/leetcode_two_sum.c +++ b/codes/c/chapter_searching/leetcode_two_sum.c @@ -82,5 +82,6 @@ int main() { res = twoSumHashTable(nums, sizeof(nums) / sizeof(int), target, &returnSize); printf("方法二 res = "); printArray(res, returnSize); + return 0; -} \ No newline at end of file +} diff --git a/codes/c/chapter_sorting/CMakeLists.txt b/codes/c/chapter_sorting/CMakeLists.txt index bf1afaff..bbe2d9b3 100644 --- a/codes/c/chapter_sorting/CMakeLists.txt +++ b/codes/c/chapter_sorting/CMakeLists.txt @@ -1,8 +1,6 @@ add_executable(bubble_sort bubble_sort.c) -add_executable(bucket_sort bucket_sort.c) add_executable(insertion_sort insertion_sort.c) add_executable(quick_sort quick_sort.c) add_executable(counting_sort counting_sort.c) add_executable(radix_sort radix_sort.c) add_executable(merge_sort merge_sort.c) - diff --git a/codes/c/chapter_sorting/bucket_sort.c b/codes/c/chapter_sorting/bucket_sort.c deleted file mode 100644 index 535dc717..00000000 --- a/codes/c/chapter_sorting/bucket_sort.c +++ /dev/null @@ -1,34 +0,0 @@ -/** - * File: bucket_sort.c - * Created Time: 2023-03-27 - * Author: Reanon (793584285@qq.com) - */ - -#include "../include/include.h" - -/* 冒泡排序 */ -void bucketSort(double nums[], int size) { - // 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素 - int k = size / 2; - // 1. 将数组元素分配到各个桶中 - // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1] - // 将 num 添加进桶 i - - // 2. 对各个桶执行排序 - - // 使用内置切片排序函数,也可以替换成其他排序算法 - - // 3. 遍历桶合并结果 -} - -/* Driver Code */ -int main() { - // 设输入数据为浮点数,范围为 [0, 1) - double nums[] = {0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37}; - int size = sizeof(nums) / sizeof(double); - bucketSort(nums, size); - - printf("桶排序完成后 nums = "); - printArray(nums, size); - -} diff --git a/codes/c/chapter_stack_and_queue/array_queue.c b/codes/c/chapter_stack_and_queue/array_queue.c index 21487b99..0cc75f8e 100644 --- a/codes/c/chapter_stack_and_queue/array_queue.c +++ b/codes/c/chapter_stack_and_queue/array_queue.c @@ -127,5 +127,6 @@ int main() { // 释放内存 delArrayQueue(queue); + return 0; } diff --git a/codes/c/chapter_stack_and_queue/linkedlist_deque.c b/codes/c/chapter_stack_and_queue/linkedlist_deque.c index 532081ad..9888849a 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_deque.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_deque.c @@ -43,6 +43,7 @@ LinkedListDeque *newLinkedListDeque() { deque->front = NULL; deque->rear = NULL; deque->queSize = 0; + return deque; } /* 析构方法 */ @@ -207,7 +208,8 @@ int main() { bool isEmpty = empty(deque); printf("双向队列是否为空 = %s\r\n", isEmpty ? "true" : "false"); - /* 释放内存 */ + // 释放内存 delLinkedListdeque(deque); + return 0; } diff --git a/codes/c/chapter_stack_and_queue/linkedlist_queue.c b/codes/c/chapter_stack_and_queue/linkedlist_queue.c index aa166ade..ef5b466d 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_queue.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_queue.c @@ -20,6 +20,7 @@ LinkedListQueue *newLinkedListQueue() { queue->front = NULL; queue->rear = NULL; queue->queSize = 0; + return queue; } /* 析构方法 */ diff --git a/codes/c/chapter_stack_and_queue/linkedlist_stack.c b/codes/c/chapter_stack_and_queue/linkedlist_stack.c index ba96d64a..e740fb48 100644 --- a/codes/c/chapter_stack_and_queue/linkedlist_stack.c +++ b/codes/c/chapter_stack_and_queue/linkedlist_stack.c @@ -109,7 +109,8 @@ int main() { bool empty = isEmpty(stack); printf("栈是否为空 = %s\n", empty ? "true" : "false"); - /* 析构方法 */ + // 释放内存 delLinkedListStack(stack); + return 0; } diff --git a/codes/c/chapter_tree/avl_tree.c b/codes/c/chapter_tree/avl_tree.c index 9c30afcb..8142979d 100644 --- a/codes/c/chapter_tree/avl_tree.c +++ b/codes/c/chapter_tree/avl_tree.c @@ -20,6 +20,7 @@ avlTree *newAVLTree() { return tree; } +/* 获取节点高度 */ int height(TreeNode *node) { // 空节点高度为 -1 ,叶节点高度为 0 if (node != NULL) { @@ -247,4 +248,6 @@ int main() { /* 查询节点 */ TreeNode *node = search(tree, 7); printf("\n查找到的节点对象节点值 = %d \n", node->val); -} \ No newline at end of file + + return 0; +} diff --git a/codes/c/chapter_tree/binary_search_tree.c b/codes/c/chapter_tree/binary_search_tree.c index c5697f51..34f81b23 100644 --- a/codes/c/chapter_tree/binary_search_tree.c +++ b/codes/c/chapter_tree/binary_search_tree.c @@ -13,8 +13,8 @@ struct binarySearchTree { typedef struct binarySearchTree binarySearchTree; +/* 比较器:从小到大排序 */ int sortIntHelper(const void *a, const void *b) { - // 从小到大排序 return (*(int *)a - *(int *)b); } diff --git a/codes/c/include/list_node.h b/codes/c/include/list_node.h index 3c5eaf73..cebe4683 100644 --- a/codes/c/include/list_node.h +++ b/codes/c/include/list_node.h @@ -28,12 +28,7 @@ ListNode *newListNode(int val) { return node; } -/** - * @brief Generate a linked list with a vector - * - * @param list - * @return ListNode* - */ +/* Generate a linked list with a vector */ ListNode *arrToLinkedList(const int *arr, size_t size) { if (size <= 0) { return NULL; @@ -48,13 +43,7 @@ ListNode *arrToLinkedList(const int *arr, size_t size) { return dummy->next; } -/** - * @brief Get a list node with specific value from a linked list - * - * @param head - * @param val - * @return ListNode* - */ +/* Get a list node with specific value from a linked list */ ListNode *getListNode(ListNode *head, int val) { while (head != NULL && head->val != val) { head = head->next; diff --git a/codes/c/include/print_util.h b/codes/c/include/print_util.h index 9cdb3f64..b38a8249 100644 --- a/codes/c/include/print_util.h +++ b/codes/c/include/print_util.h @@ -18,12 +18,7 @@ extern "C" { #endif -/** - * @brief Print an Array - * - * @param arr - * @param size - */ +/* Print an Array */ static void printArray(int arr[], int size) { printf("["); if (arr != NULL && size != 0) { @@ -44,11 +39,7 @@ static void printArray(int arr[], int size) { } } -/** - * @brief Print a linked list - * - * @param head - */ +/* Print a linked list */ static void printLinkedList(ListNode *node) { if (node == NULL) { return; @@ -75,11 +66,7 @@ Trunk *newTrunk(Trunk *prev, char *str) { return trunk; } -/** - * @brief Helper function to print branches of the binary tree - * - * @param trunk - */ +/* Helper function to print branches of the binary tree */ void showTrunks(Trunk *trunk) { if (trunk == NULL) { return; @@ -88,12 +75,7 @@ void showTrunks(Trunk *trunk) { printf("%s", trunk->str); } -/** - * Help to print a binary tree, hide more details - * @param node - * @param prev - * @param isLeft - */ +/* Help to print a binary tree, hide more details */ static void printTreeHelper(TreeNode *node, Trunk *prev, bool isLeft) { if (node == NULL) { return; @@ -121,21 +103,12 @@ static void printTreeHelper(TreeNode *node, Trunk *prev, bool isLeft) { printTreeHelper(node->left, trunk, false); } -/** - * @brief Print a binary tree - * - * @param head - */ +/* Print a binary tree */ static void printTree(TreeNode *root) { printTreeHelper(root, NULL, false); } -/** - * @brief Print a Heap - * - * @param arr - * @param size - */ +/* Print a Heap */ static void printHeap(int arr[], int size) { TreeNode *root; printf("堆的数组表示:"); diff --git a/codes/c/include/tree_node.h b/codes/c/include/tree_node.h index 5e03c1f7..7a8b5584 100644 --- a/codes/c/include/tree_node.h +++ b/codes/c/include/tree_node.h @@ -34,13 +34,7 @@ TreeNode *newTreeNode(int val) { return node; } -/** - * @brief Generate a binary tree with an array - * - * @param arr - * @param size - * @return TreeNode * - */ +/* Generate a binary tree with an array */ TreeNode *arrToTree(const int *arr, size_t size) { if (size <= 0) { return NULL; @@ -81,13 +75,7 @@ TreeNode *arrToTree(const int *arr, size_t size) { return root; } -/** - * @brief Generate a binary tree with an array - * - * @param arr - * @param size - * @return TreeNode * - */ +/* Generate a binary tree with an array */ int *treeToArr(TreeNode *root) { if (root == NULL) { return NULL;