给stack添加迭代器,但为什么heap会报错??

This commit is contained in:
建峰 2025-04-24 11:13:11 +08:00
parent d805d56fc9
commit 07b2c142b8
5 changed files with 87 additions and 25 deletions

View File

@ -11,9 +11,11 @@
#ifndef _COMMON_H_
#define _COMMON_H_
// #ifdef UNICSTL_CONFIG
#define UNICSTL_CONFIG
#ifdef UNICSTL_CONFIG
#include "unicstl_config.h"
// #endif
#endif
#include <stdint.h>
#include <stdbool.h>
@ -23,4 +25,8 @@
#include <limits.h>
#include <assert.h>
#ifdef UNICSTL_ITERATOR
#include "iterator.h"
#endif
#endif // _COMMON_H_

View File

@ -21,6 +21,7 @@ struct _stack_node
struct _stack
{
// ---------- private ----------
struct _stack_node * _head;
uint32_t _size;
@ -28,21 +29,26 @@ struct _stack
uint32_t _capacity;
uint32_t _ratio;
struct _iterator _iter;
void (*destory)(struct _stack* self);
// ---------- public ----------
// kernel
bool (*push)(struct _stack* self, void* obj);
bool (*pop)(struct _stack* self, void* obj);
bool (*peek)(struct _stack* self, void* obj);
bool (*empty)(struct _stack* self);
// base
uint32_t (*size)(struct _stack* self);
bool (*empty)(struct _stack* self);
uint32_t (*capacity)(struct _stack* self);
// clear and free node
bool (*clear)(struct _stack* self);
void (*destory)(struct _stack* self);
// iter
iterator_t (*iter)(struct _stack* self);
// print
// ---------- debug ----------
void (*print)(struct _stack* self);
void (*print_obj)(void* obj);
};

View File

@ -21,6 +21,7 @@
#define UNICSTL_TREE
#define UNICSTL_HEAP
#define UNICSTL_GRAPH
#define UNICSTL_ITERATOR
/**
* @brief debug
@ -29,5 +30,14 @@
#define NDEBUG
#define UNICSTL_DEBUG
#ifdef UNICSTL_DEBUG
#define UNICSTL_DEBUG_STACK
#define UNICSTL_DEBUG_QUEUE
#define UNICSTL_DEBUG_DEQUE
#define UNICSTL_DEBUG_TREE
#define UNICSTL_DEBUG_HEAP
#define UNICSTL_DEBUG_GRAPH
// #define UNICSTL_DEBUG_ITERATOR
#endif
#endif

View File

@ -298,48 +298,87 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
return true;
}
const void* stack_iter_next(struct _iterator* iter)
{
stack_t self = (stack_t)iter->parent;
void *obj = self->_head->obj + self->_iter._cur * self->_obj_size;
self->_iter._cur += 1;
return obj;
}
bool stack_iter_hasnext(struct _iterator* iter)
{
stack_t self = (stack_t)iter->parent;
if(self->_iter._cur < self->size(self))
{
return true;
}
return false;
}
iterator_t stack_iter(struct _stack* self)
{
assert(self != NULL);
if (self == NULL)
{
return NULL;
}
self->_iter._cur = 0;
return &self->_iter;
}
static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
{
assert(self != NULL);
// ---------- private ----------
// 1. set attr
self->_obj_size = obj_size;
self->_size = 0;
self->_capacity = capacity;
self->_ratio = 2;
// 2. set function
// kernel
self->push = stack2_push;
self->pop = stack2_pop;
self->peek = stack2_peek;
// others
self->empty = stack_empty;
self->size = stack_size;
self->capacity = stack_capacity;
self->_iter.next = stack_iter_next;
self->_iter.hasnext = stack_iter_hasnext;
// clear and free node
self->clear = stack_clear;
self->destory = stack2_destory;
// print
self->print = stack2_print;
// 3. set node
self->_head = (struct _stack_node*)malloc(sizeof(struct _stack_node));
if (self->_head == NULL)
{
return false;
}
// self->_head->obj = NULL;
self->_head->next = NULL;
// 4. set array
self->_head->obj = (void *)calloc(self->_capacity, self->_obj_size);
if (self->_head->obj == NULL)
{
free(self->_head);
self->_head = NULL;
return false;
}
self->destory = stack2_destory;
// ---------- public ----------
// 2. set function
// kernel
self->push = stack2_push;
self->pop = stack2_pop;
self->peek = stack2_peek;
self->empty = stack_empty;
// base
self->size = stack_size;
self->capacity = stack_capacity;
self->clear = stack_clear;
// iter
self->iter = stack_iter;
// ---------- debug ----------
self->print = stack2_print;
return true;
}

View File

@ -107,6 +107,7 @@ static void test_heap_struct(void)
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
TEST_ASSERT_TRUE(heap->peek(heap, &temp));
// debug Why is it occasionally incorrect here?
TEST_ASSERT_EQUAL_INT(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->id, temp.id);
TEST_ASSERT_EQUAL_STRING(((struct _student*)get_min(heap, data, 0, heap->size(heap)))->name, temp.name);