Fine tune the C codes.

pull/469/head
krahets 2 years ago
parent c4ea4e39f3
commit af0f92c18d

@ -160,4 +160,4 @@ int main() {
delMyList(list);
return 0;
}
}

@ -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;
}

@ -142,5 +142,6 @@ int main() {
if (node)
printf("目标节点值 3 的对应节点对象为 %#p val: %d\r\n", node, node->val);
deleteAll(ht1);
return 0;
}

@ -82,5 +82,6 @@ int main() {
res = twoSumHashTable(nums, sizeof(nums) / sizeof(int), target, &returnSize);
printf("方法二 res = ");
printArray(res, returnSize);
return 0;
}
}

@ -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)

@ -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);
}

@ -127,5 +127,6 @@ int main() {
// 释放内存
delArrayQueue(queue);
return 0;
}

@ -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;
}

@ -20,6 +20,7 @@ LinkedListQueue *newLinkedListQueue() {
queue->front = NULL;
queue->rear = NULL;
queue->queSize = 0;
return queue;
}
/* 析构方法 */

@ -109,7 +109,8 @@ int main() {
bool empty = isEmpty(stack);
printf("栈是否为空 = %s\n", empty ? "true" : "false");
/* 析构方法 */
// 释放内存
delLinkedListStack(stack);
return 0;
}

@ -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);
}
return 0;
}

@ -13,8 +13,8 @@ struct binarySearchTree {
typedef struct binarySearchTree binarySearchTree;
/* 比较器:从小到大排序 */
int sortIntHelper(const void *a, const void *b) {
// 从小到大排序
return (*(int *)a - *(int *)b);
}

@ -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;

@ -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("堆的数组表示:");

@ -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;

Loading…
Cancel
Save