From 6a40df3ad1af384ac09eba6f45e7c4627320ff62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Thu, 3 Aug 2023 19:48:02 +0200 Subject: [PATCH] feat: add linked_list_free --- lib/linked_list.c | 10 ++++++++++ lib/linked_list.h | 6 ++++++ test/linked_list_test.c | 6 ++++++ 3 files changed, 22 insertions(+) diff --git a/lib/linked_list.c b/lib/linked_list.c index 6b75b4e..e3298d7 100644 --- a/lib/linked_list.c +++ b/lib/linked_list.c @@ -84,3 +84,13 @@ void linked_list_reverse_mutate(struct linked_list *list) { (*current) = temporary_current; } } + +void linked_list_free(struct linked_list *list) { + struct linked_list_node *node_current = list->head; + while (node_current != NULL) { + struct linked_list_node *node_to_remove = node_current; + node_current = node_current->next; + free(node_to_remove); + } + free(list); +} diff --git a/lib/linked_list.h b/lib/linked_list.h index 82649d7..34818d4 100644 --- a/lib/linked_list.h +++ b/lib/linked_list.h @@ -62,4 +62,10 @@ struct linked_list *linked_list_reverse(struct linked_list *list); */ void linked_list_reverse_mutate(struct linked_list *list); +/** + * @brief Free the linked list. + * @since v2.1.0 + */ +void linked_list_free(struct linked_list *list); + #endif diff --git a/test/linked_list_test.c b/test/linked_list_test.c index 29710b1..197e1ca 100644 --- a/test/linked_list_test.c +++ b/test/linked_list_test.c @@ -13,6 +13,7 @@ void linked_list_initialization_test() { struct linked_list *list = linked_list_initialization(); assert(list->length == 0); assert(list->head == NULL); + linked_list_free(list); } void linked_list_add_in_head_test() { @@ -25,6 +26,7 @@ void linked_list_add_in_head_test() { assert(((uintptr_t)list->head->next->data) == 8); assert(((uintptr_t)list->head->next->next->data) == 4); assert(list->head->next->next->next == NULL); + linked_list_free(list); } void linked_list_delete_in_head_test() { @@ -37,6 +39,7 @@ void linked_list_delete_in_head_test() { assert(((uintptr_t)list->head->data) == 8); assert(((uintptr_t)list->head->next->data) == 4); assert(list->head->next->next == NULL); + linked_list_free(list); } void linked_list_add_after_last_test() { @@ -54,6 +57,7 @@ void linked_list_add_after_last_test() { assert(((uintptr_t)list->head->next->data) == 8); assert(((uintptr_t)list->head->next->next->data) == 4); assert(((uintptr_t)list->head->next->next->next->data) == 18); + linked_list_free(list); } void linked_list_reverse_test() { @@ -74,6 +78,7 @@ void linked_list_reverse_test() { assert((list_reversed->head->next->data) == (void *)'C'); assert((list_reversed->head->next->next->data) == (void *)'B'); assert((list_reversed->head->next->next->next->data) == (void *)'A'); + linked_list_free(list); } void linked_list_reverse_mutate_test() { @@ -89,4 +94,5 @@ void linked_list_reverse_mutate_test() { assert((list->head->next->data) == (void *)'C'); assert((list->head->next->next->data) == (void *)'B'); assert((list->head->next->next->next->data) == (void *)'A'); + linked_list_free(list); }