From 40b5be53021d459ad22263b4dcc8e56aff5784fa Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 12 Jan 2023 00:31:11 +0800 Subject: [PATCH] Unify the naming of the C codes. --- codes/c/chapter_array_and_linkedlist/array.c | 10 +++++----- .../worst_best_time_complexity.c | 2 +- codes/c/chapter_sorting/bubble_sort.c | 8 ++++---- codes/c/chapter_tree/binary_tree.c | 19 +++++++++---------- codes/c/chapter_tree/binary_tree_bfs.c | 6 +++--- codes/c/chapter_tree/binary_tree_dfs.c | 12 ++++++------ codes/c/include/include_test.c | 14 +++++++------- codes/c/include/list_node.h | 10 +++++----- codes/c/include/print_util.h | 6 +++--- codes/c/include/tree_node.h | 12 ++++++------ docs/chapter_sorting/bubble_sort.md | 4 ++-- 11 files changed, 51 insertions(+), 52 deletions(-) diff --git a/codes/c/chapter_array_and_linkedlist/array.c b/codes/c/chapter_array_and_linkedlist/array.c index 11fcb67f9..33dc99903 100644 --- a/codes/c/chapter_array_and_linkedlist/array.c +++ b/codes/c/chapter_array_and_linkedlist/array.c @@ -74,11 +74,11 @@ int main() { int size = 5; int arr[5]; printf("数组 arr = "); - PrintArray(arr, size); + printArray(arr, size); int nums[5] = { 1, 3, 2, 5, 4 }; printf("数组 nums = "); - PrintArray(nums, size); + printArray(nums, size); /* 随机访问 */ int randomNum = randomAccess(nums, size); @@ -89,17 +89,17 @@ int main() { int* res = extend(nums, size, enlarge); size += enlarge; printf("将数组长度扩展至 8 ,得到 nums = "); - PrintArray(res, size); + printArray(res, size); /* 插入元素 */ insert(res, size, 6, 3); printf("在索引 3 处插入数字 6 ,得到 nums = "); - PrintArray(res, size); + printArray(res, size); /* 删除元素 */ removeItem(res, size, 2); printf("删除索引 2 处的元素,得到 nums = "); - PrintArray(res, size); + printArray(res, size); /* 遍历数组 */ traverse(res, size); diff --git a/codes/c/chapter_computational_complexity/worst_best_time_complexity.c b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c index 531a76e51..e5cee821f 100644 --- a/codes/c/chapter_computational_complexity/worst_best_time_complexity.c +++ b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) { int *nums = randomNumbers(n); int index = findOne(nums, n); printf("\n数组 [ 1, 2, ..., n ] 被打乱后 = "); - PrintArray(nums, n); + printArray(nums, n); printf("数字 1 的索引为 %d\n", index); // 释放堆区内存 if (nums != NULL) { diff --git a/codes/c/chapter_sorting/bubble_sort.c b/codes/c/chapter_sorting/bubble_sort.c index 4a2a24b44..51bab6c64 100644 --- a/codes/c/chapter_sorting/bubble_sort.c +++ b/codes/c/chapter_sorting/bubble_sort.c @@ -7,7 +7,7 @@ #include "../include/include.h" /* 冒泡排序 */ -void bubble_sort(int nums[], int size) { +void bubbleSort(int nums[], int size) { // 外循环:待排序元素数量为 n-1, n-2, ..., 1 for (int i = 0; i < size - 1; i++) { @@ -25,7 +25,7 @@ void bubble_sort(int nums[], int size) { } /* 冒泡排序(标志优化)*/ -void bubble_sort_with_flag(int nums[], int size) { +void bubbleSortWithFlag(int nums[], int size) { // 外循环:待排序元素数量为 n-1, n-2, ..., 1 for (int i = 0; i < size - 1; i++) { @@ -51,14 +51,14 @@ void bubble_sort_with_flag(int nums[], int size) { int main() { int nums[6] = {4, 1, 3, 1, 5, 2}; printf("冒泡排序后: "); - bubble_sort(nums, 6); + bubbleSort(nums, 6); for (int i = 0; i < 6; i++) { printf("%d ", nums[i]); } printf("\n优化版冒泡排序后: "); - bubble_sort_with_flag(nums, 6); + bubbleSortWithFlag(nums, 6); for (int i = 0; i < 6; i++) { printf("%d ", nums[i]); diff --git a/codes/c/chapter_tree/binary_tree.c b/codes/c/chapter_tree/binary_tree.c index 0a2f946a7..1eb8e846a 100644 --- a/codes/c/chapter_tree/binary_tree.c +++ b/codes/c/chapter_tree/binary_tree.c @@ -10,34 +10,33 @@ int main() { /* 初始化二叉树 */ // 初始化结点 - TreeNode* n1 = NewTreeNode(1); - TreeNode* n2 = NewTreeNode(2); - TreeNode* n3 = NewTreeNode(3); - TreeNode* n4 = NewTreeNode(4); - TreeNode* n5 = NewTreeNode(5); + TreeNode* n1 = newTreeNode(1); + TreeNode* n2 = newTreeNode(2); + TreeNode* n3 = newTreeNode(3); + TreeNode* n4 = newTreeNode(4); + TreeNode* n5 = newTreeNode(5); // 构建引用指向(即指针) n1->left = n2; n1->right = n3; n2->left = n4; n2->right = n5; printf("初始化二叉树\n"); - PrintTree(n1); + printTree(n1); /* 插入与删除结点 */ - TreeNode* P = NewTreeNode(0); + TreeNode* P = newTreeNode(0); // 在 n1 -> n2 中间插入结点 P n1->left = P; P->left = n2; printf("插入结点 P 后\n"); - PrintTree(n1); + printTree(n1); // 删除结点 P n1->left = n2; // 释放内存 free(P); printf("删除结点 P 后\n"); - PrintTree(n1); + printTree(n1); return 0; } - diff --git a/codes/c/chapter_tree/binary_tree_bfs.c b/codes/c/chapter_tree/binary_tree_bfs.c index abad3735f..c7b83b912 100644 --- a/codes/c/chapter_tree/binary_tree_bfs.c +++ b/codes/c/chapter_tree/binary_tree_bfs.c @@ -52,15 +52,15 @@ int main() { // 这里借助了一个从数组直接生成二叉树的函数 int nums[] = {1, 2, 3, NIL, 5, 6, NIL}; int size = sizeof(nums) / sizeof(int); - TreeNode *root = ArrayToTree(nums, size); + TreeNode *root = arrToTree(nums, size); printf("初始化二叉树\n"); - PrintTree(root); + printTree(root); /* 层序遍历 */ // 需要传入数组的长度 int *arr = levelOrder(root, &size); printf("层序遍历的结点打印序列 = "); - PrintArray(arr, size); + printArray(arr, size); return 0; } \ No newline at end of file diff --git a/codes/c/chapter_tree/binary_tree_dfs.c b/codes/c/chapter_tree/binary_tree_dfs.c index f72e6687b..bafc90fd1 100644 --- a/codes/c/chapter_tree/binary_tree_dfs.c +++ b/codes/c/chapter_tree/binary_tree_dfs.c @@ -43,10 +43,10 @@ int main() { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 int nums[] = {1, 2, 3, 4, 5, 6, 7}; - int size = sizeof(nums) / sizt ceof(int); - TreeNode *root = ArrayToTree(nums, size); + int size = sizeof(nums) / sizeof(int); + TreeNode *root = arrToTree(nums, size); printf("初始化二叉树\n"); - PrintTree(root); + printTree(root); /* 前序遍历 */ // 初始化辅助数组 @@ -54,19 +54,19 @@ int main() { size = 0; preOrder(root, &size); printf("前序遍历的结点打印序列 = "); - PrintArray(arr, size); + printArray(arr, size); /* 中序遍历 */ size = 0; inOrder(root, &size); printf("中序遍历的结点打印序列 = "); - PrintArray(arr, size); + printArray(arr, size); /* 后序遍历 */ size = 0; postOrder(root, &size); printf("后序遍历的结点打印序列 = "); - PrintArray(arr, size); + printArray(arr, size); return 0; } diff --git a/codes/c/include/include_test.c b/codes/c/include/include_test.c index baa8e649d..602670d68 100644 --- a/codes/c/include/include_test.c +++ b/codes/c/include/include_test.c @@ -9,24 +9,24 @@ void testListNode() { int nums[] = {2, 3, 5, 6, 7}; int size = sizeof(nums) / sizeof(int); - ListNode *head = ArrayToLinkedList(nums, size); - PrintLinkedList(head); + ListNode *head = arrToLinkedList(nums, size); + printLinkedList(head); - ListNode *node = GetListNode(head, 5); + ListNode *node = getListNode(head, 5); printf("find node: %d\n", node->val); } void testTreeNode() { int nums[] = {1, 2, 3, NIL, 5, 6, NIL}; int size = sizeof(nums) / sizeof(int); - TreeNode *root = ArrayToTree(nums, size); + TreeNode *root = arrToTree(nums, size); // print tree - PrintTree(root); + printTree(root); // tree to arr - int *arr = TreeToArray(root); - PrintArray(arr, size); + int *arr = treeToArr(root); + printArray(arr, size); } int main(int argc, char *argv[]) { diff --git a/codes/c/include/list_node.h b/codes/c/include/list_node.h index 5597d2102..1845ffafd 100644 --- a/codes/c/include/list_node.h +++ b/codes/c/include/list_node.h @@ -22,7 +22,7 @@ struct ListNode { // typedef 为 C 语言的关键字,作用是为一种数据类型定义一个新名字 typedef struct ListNode ListNode; -ListNode *NewListNode(int val) { +ListNode *newListNode(int val) { ListNode *node, *next; node = (ListNode *) malloc(sizeof(ListNode)); node->val = val; @@ -37,15 +37,15 @@ ListNode *NewListNode(int val) { * @return ListNode* */ -ListNode *ArrayToLinkedList(const int *arr, size_t size) { +ListNode *arrToLinkedList(const int *arr, size_t size) { if (size <= 0) { return NULL; } - ListNode *dummy = NewListNode(0); + ListNode *dummy = newListNode(0); ListNode *node = dummy; for (int i = 0; i < size; i++) { - node->next = NewListNode(arr[i]); + node->next = newListNode(arr[i]); node = node->next; } return dummy->next; @@ -58,7 +58,7 @@ ListNode *ArrayToLinkedList(const int *arr, size_t size) { * @param val * @return ListNode* */ -ListNode *GetListNode(ListNode *head, int val) { +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 fcb0a4ca6..a0c3a1500 100644 --- a/codes/c/include/print_util.h +++ b/codes/c/include/print_util.h @@ -25,7 +25,7 @@ extern "C" { * @param arr * @param size */ -static void PrintArray(int *arr, int size) { +static void printArray(int *arr, int size) { printf("["); for (int i = 0; i < size - 1; i++) { if (arr[i] != NIL) { @@ -46,7 +46,7 @@ static void PrintArray(int *arr, int size) { * * @param head */ -static void PrintLinkedList(ListNode *node) { +static void printLinkedList(ListNode *node) { if (node == NULL) { return; } @@ -123,7 +123,7 @@ static void printTreeHelper(TreeNode *node, Trunk *prev, bool isLeft) { * * @param head */ -static void PrintTree(TreeNode *root) { +static void printTree(TreeNode *root) { printTreeHelper(root, NULL, false); } diff --git a/codes/c/include/tree_node.h b/codes/c/include/tree_node.h index 4b0e70187..c0438c437 100644 --- a/codes/c/include/tree_node.h +++ b/codes/c/include/tree_node.h @@ -25,7 +25,7 @@ struct TreeNode { typedef struct TreeNode TreeNode; -TreeNode *NewTreeNode(int val) { +TreeNode *newTreeNode(int val) { TreeNode *node; node = (TreeNode *) malloc(sizeof(TreeNode)); @@ -43,7 +43,7 @@ TreeNode *NewTreeNode(int val) { * @param size * @return TreeNode * */ -TreeNode *ArrayToTree(const int *arr, size_t size) { +TreeNode *arrToTree(const int *arr, size_t size) { if (size <= 0) { return NULL; } @@ -53,7 +53,7 @@ TreeNode *ArrayToTree(const int *arr, size_t size) { TreeNode **queue; /* 根结点 */ - root = NewTreeNode(arr[0]); + root = newTreeNode(arr[0]); /* 辅助队列 */ queue = (TreeNode **) malloc(sizeof(TreeNode) * MAX_NODE_SIZE); // 队列指针 @@ -68,14 +68,14 @@ TreeNode *ArrayToTree(const int *arr, size_t size) { index++; if (index < size) { if (arr[index] != NIL) { - node->left = NewTreeNode(arr[index]); + node->left = newTreeNode(arr[index]); queue[rear++] = node->left; } } index++; if (index < size) { if (arr[index] != NIL) { - node->right = NewTreeNode(arr[index]); + node->right = newTreeNode(arr[index]); queue[rear++] = node->right; } } @@ -91,7 +91,7 @@ TreeNode *ArrayToTree(const int *arr, size_t size) { * @param size * @return TreeNode * */ -int *TreeToArray(TreeNode *root) { +int *treeToArr(TreeNode *root) { if (root == NULL) { return NULL; } diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index e3e394b20..3bb0bc994 100644 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -170,7 +170,7 @@ comments: true ```c title="bubble_sort.c" /* 冒泡排序 */ - void bubble_sort(int nums[], int size) { + void bubbleSort(int nums[], int size) { // 外循环:待排序元素数量为 n-1, n-2, ..., 1 for (int i = 0; i < size - 1; i++) { @@ -373,7 +373,7 @@ comments: true ```c title="bubble_sort.c" /* 冒泡排序 */ - void bubble_sort(int nums[], int size) { + void bubbleSortWithFlag(int nums[], int size) { // 外循环:待排序元素数量为 n-1, n-2, ..., 1 for (int i = 0; i < size - 1; i++) {