栈的迭代器从栈顶到栈底,另外destory函数私有化

This commit is contained in:
建峰 2025-04-24 11:36:56 +08:00
parent b396d80672
commit 86855420e2
4 changed files with 11 additions and 8 deletions

View File

@ -200,7 +200,7 @@ static void demo_stack_struct(void)
}
}
stack->destory(stack);
stack->_destory(stack);
stack_free(&stack);
}

View File

@ -31,7 +31,7 @@ struct _stack
struct _iterator _iter;
void (*destory)(struct _stack* self);
void (*_destory)(struct _stack* self);
// ---------- public ----------
// kernel

View File

@ -282,7 +282,7 @@ static bool stack_init(struct _stack* self, uint32_t obj_size)
// clear and free node
self->clear = stack_clear;
self->destory = stack_destory;
self->_destory = stack_destory;
// print
self->print = stack_print;
@ -304,7 +304,10 @@ const void* stack_iter_next(struct _iterator* iter)
assert(iter->parent != NULL);
stack_t self = (stack_t)iter->parent;
void *obj = self->_head->obj + self->_iter._cur * self->_obj_size;
// from top to bottom
uint32_t index = self->size(self) - 1 - self->_iter._cur;
void *obj = self->_head->obj + self->_obj_size * index;
self->_iter._cur += 1;
return obj;
}
@ -360,7 +363,7 @@ static bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacit
return false;
}
self->destory = stack2_destory;
self->_destory = stack2_destory;
// ---------- public ----------
// 2. set function
@ -418,9 +421,9 @@ void stack_free(stack_t *stack)
assert(stack != NULL);
if(stack != NULL && *stack != NULL)
{
if((*stack)->destory != NULL)
if((*stack)->_destory != NULL)
{
(*stack)->destory(*stack);
(*stack)->_destory(*stack);
}
free(*stack);
*stack = NULL;

View File

@ -470,7 +470,7 @@ static void test_stack2_iter(void)
while(iter->hasnext(iter))
{
temp = *(int *)iter->next(iter);
printf("%d ", temp);
// printf("%d ", temp);
TEST_ASSERT_EQUAL_INT(data[len - 1 - i], temp);
i++;
}