1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-08 22:31:31 +01:00

feat: add stack_free

This commit is contained in:
Théo LUDWIG 2023-08-03 23:01:19 +02:00
parent 1e475a59b1
commit 8b6f06dc6e
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
3 changed files with 22 additions and 0 deletions

View File

@ -35,3 +35,16 @@ void *stack_pop(struct stack *stack) {
stack->length = stack->length - 1;
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);
}

View File

@ -42,4 +42,10 @@ void stack_push(struct stack *stack, void *data);
*/
void *stack_pop(struct stack *stack);
/**
* @brief Frees the stack.
* @since v2.1.0
*/
void stack_free(struct stack *stack);
#endif

View File

@ -10,6 +10,7 @@ void stack_initialization_test() {
struct stack *stack = stack_initialization();
assert(stack->length == 0);
assert(stack->first == NULL);
stack_free(stack);
}
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->next->data) == 4);
assert(stack->first->next->next->next == NULL);
stack_free(stack);
}
void stack_pop_test() {
@ -35,4 +37,5 @@ void stack_pop_test() {
assert(((uintptr_t)stack->first->data) == 8);
assert(((uintptr_t)stack->first->next->data) == 4);
assert(stack->first->next->next == NULL);
stack_free(stack);
}