From e49add3f163bc5853a94cf2b6e22b5c71249f53c Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Wed, 23 Apr 2025 13:47:55 +0800 Subject: [PATCH] =?UTF-8?q?heap=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 +++++++------- demo/demo_heap.c | 6 ++---- include/heap.h | 4 ++-- src/heap.c | 34 ++++++++++++++++++++++------------ test/test_heap.c | 8 ++------ 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index a737f43..b2b3c87 100644 --- a/README.md +++ b/README.md @@ -16,17 +16,17 @@ | stack_new | 链表 | | | stack_new2 | 动态数组 | | | **list** | | **列表** -| list_init2 | 动态数组 | | +| list_new2 | 动态数组 | | | **queue** | | **队列** -| queue_init | 单向链表 | | -| queue_init2 | 数组 | FIFO/空/满 | +| queue_new | 单向链表 | | +| queue_new2 | 数组 | FIFO/空/满 | | **deque** | |**双端队列** | -| deque_init | 双向循环链表 | | +| deque_new | 双向循环链表 | | | **tree** | |**树** | -| tree_avl_init | 二叉搜索树 | AVL树 | -| tree_rb_init | 二叉搜索树 | 红黑树 | +| tree_avl_new | 二叉搜索树 | AVL树 | +| tree_rb_new | 二叉搜索树 | 红黑树 | | **heap** | |**堆** | -| heap_init2 | 数组 | 最大堆/最小堆 | +| heap_new2 | 数组 | 最大堆/最小堆 | ## 特点 | 原理 | 说明 | diff --git a/demo/demo_heap.c b/demo/demo_heap.c index 02cae81..47ad134 100644 --- a/demo/demo_heap.c +++ b/demo/demo_heap.c @@ -20,8 +20,7 @@ void demo_heap_num(void) int temp = 0; uint32_t len = sizeof(data) / sizeof(data[0]); - heap_t heap = heap_new(); - heap_init2(heap, sizeof(int), 64); + heap_t heap = heap_new2(sizeof(int), 64); heap->print_obj = print_num; heap->compare = compare_num; @@ -91,8 +90,7 @@ static void demo_heap_struct(void) struct _student temp = {0}; uint32_t len = sizeof(data) / sizeof(data[0]); - heap_t heap = heap_new(); - heap_init2(heap, sizeof(struct _student), 64); + heap_t heap = heap_new2(sizeof(struct _student), 64); heap->print_obj = print_struct; heap->compare = compare_struct; diff --git a/include/heap.h b/include/heap.h index d12705d..c162c1c 100644 --- a/include/heap.h +++ b/include/heap.h @@ -56,9 +56,9 @@ struct _heap }; typedef struct _heap* heap_t; -bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity); +// create and free heap +heap_t heap_new2(uint32_t obj_size, uint32_t capacity); -heap_t heap_new(void); void heap_free(heap_t* heap); #endif // _HEAP_H_ diff --git a/src/heap.c b/src/heap.c index 7dd7707..6e4cfb8 100644 --- a/src/heap.c +++ b/src/heap.c @@ -27,7 +27,7 @@ static int parent(int i) return (i-1) >> 1; } -bool heap_peek(struct _heap* self, void* obj) +static bool heap_peek(struct _heap* self, void* obj) { assert(self != NULL); assert(obj != NULL); @@ -101,7 +101,7 @@ static void heap_fixed_up(struct _heap* self, int i) } } -bool heap_push(struct _heap* self, void* obj) +static bool heap_push(struct _heap* self, void* obj) { assert(self != NULL); if(self->size(self) > self->_capacity) @@ -174,7 +174,7 @@ static void heap_fixed_down(struct _heap* self, int i) } } -bool heap_pop(struct _heap* self, void* obj) +static bool heap_pop(struct _heap* self, void* obj) { assert(self != NULL); if(self->empty(self)) @@ -192,32 +192,32 @@ bool heap_pop(struct _heap* self, void* obj) return true; } -void heap_setmin(struct _heap* self, bool min_flag) +static void heap_setmin(struct _heap* self, bool min_flag) { assert(self != NULL); self->_min_flag = min_flag; } -uint32_t heap_size(struct _heap* self) +static uint32_t heap_size(struct _heap* self) { assert(self != NULL); return self->_size; } -bool heap_empty(struct _heap* self) +static bool heap_empty(struct _heap* self) { assert(self != NULL); return self->size(self) == 0; } -bool heap_clear(struct _heap* self) +static bool heap_clear(struct _heap* self) { assert(self != NULL); self->_size = 0; return true; } -void heap_destory(struct _heap* self) +static void heap_destory(struct _heap* self) { assert(self != NULL); self->clear(self); @@ -227,7 +227,7 @@ void heap_destory(struct _heap* self) } } -void heap_print(struct _heap* self) +static void heap_print(struct _heap* self) { assert(self != NULL); assert(self->obj != NULL); @@ -243,7 +243,7 @@ void heap_print(struct _heap* self) } } -bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) +static bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) { assert(self != NULL); @@ -271,9 +271,19 @@ bool heap_init2(struct _heap* self, uint32_t obj_size, uint32_t capacity) return true; } -heap_t heap_new(void) +heap_t heap_new2(uint32_t obj_size, uint32_t capacity) { - return (struct _heap*)malloc(sizeof(struct _heap)); + heap_t heap = NULL; + heap = (struct _heap*)malloc(sizeof(struct _heap)); + if(heap != NULL) + { + if(heap_init2(heap, obj_size, capacity) != true) + { + free(heap); + heap = NULL; + } + } + return heap; } void heap_free(heap_t* heap) diff --git a/test/test_heap.c b/test/test_heap.c index 4b8bc32..4d12a5b 100644 --- a/test/test_heap.c +++ b/test/test_heap.c @@ -46,10 +46,8 @@ static void test_heap_num(void) int temp = 0; uint32_t len = sizeof(data) / sizeof(data[0]); - heap_t heap = heap_new(); + heap_t heap = heap_new2(sizeof(int), 64); TEST_ASSERT_NOT_NULL(heap); - - heap_init2(heap, sizeof(int), 64); heap->print_obj = print_num; heap->compare = compare_num; @@ -95,10 +93,8 @@ static void test_heap_struct(void) struct _student temp = {0}; uint32_t len = sizeof(data) / sizeof(data[0]); - heap_t heap = heap_new(); + heap_t heap = heap_new2(sizeof(struct _student), 64); TEST_ASSERT_NOT_NULL(heap); - - heap_init2(heap, sizeof(struct _student), 64); heap->print_obj = print_struct; heap->compare = compare_struct;