mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-11-13 23:43:13 +01:00
feat: add stack_free
This commit is contained in:
parent
1e475a59b1
commit
8b6f06dc6e
13
lib/stack.c
13
lib/stack.c
@ -35,3 +35,16 @@ void *stack_pop(struct stack *stack) {
|
|||||||
stack->length = stack->length - 1;
|
stack->length = stack->length - 1;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void stack_free(struct stack *stack) {
|
||||||
|
if (stack == NULL) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
struct stack_node *node = stack->first;
|
||||||
|
while (node != NULL) {
|
||||||
|
struct stack_node *node_next = node->next;
|
||||||
|
free(node);
|
||||||
|
node = node_next;
|
||||||
|
}
|
||||||
|
free(stack);
|
||||||
|
}
|
||||||
|
@ -42,4 +42,10 @@ void stack_push(struct stack *stack, void *data);
|
|||||||
*/
|
*/
|
||||||
void *stack_pop(struct stack *stack);
|
void *stack_pop(struct stack *stack);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Frees the stack.
|
||||||
|
* @since v2.1.0
|
||||||
|
*/
|
||||||
|
void stack_free(struct stack *stack);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,6 +10,7 @@ void stack_initialization_test() {
|
|||||||
struct stack *stack = stack_initialization();
|
struct stack *stack = stack_initialization();
|
||||||
assert(stack->length == 0);
|
assert(stack->length == 0);
|
||||||
assert(stack->first == NULL);
|
assert(stack->first == NULL);
|
||||||
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stack_push_test() {
|
void stack_push_test() {
|
||||||
@ -22,6 +23,7 @@ void stack_push_test() {
|
|||||||
assert(((uintptr_t)stack->first->next->data) == 8);
|
assert(((uintptr_t)stack->first->next->data) == 8);
|
||||||
assert(((uintptr_t)stack->first->next->next->data) == 4);
|
assert(((uintptr_t)stack->first->next->next->data) == 4);
|
||||||
assert(stack->first->next->next->next == NULL);
|
assert(stack->first->next->next->next == NULL);
|
||||||
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stack_pop_test() {
|
void stack_pop_test() {
|
||||||
@ -35,4 +37,5 @@ void stack_pop_test() {
|
|||||||
assert(((uintptr_t)stack->first->data) == 8);
|
assert(((uintptr_t)stack->first->data) == 8);
|
||||||
assert(((uintptr_t)stack->first->next->data) == 4);
|
assert(((uintptr_t)stack->first->next->data) == 4);
|
||||||
assert(stack->first->next->next == NULL);
|
assert(stack->first->next->next == NULL);
|
||||||
|
stack_free(stack);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user