mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 22:40:25 +08:00
Unify the naming of the C codes.
This commit is contained in:
parent
9ef3395aee
commit
40b5be5302
@ -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);
|
||||
|
@ -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) {
|
||||
|
@ -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]);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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[]) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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++)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user