1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00

feat: add linked_list_free

This commit is contained in:
Théo LUDWIG 2023-08-03 19:48:02 +02:00
parent d231a0f055
commit 6a40df3ad1
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
3 changed files with 22 additions and 0 deletions

View File

@ -84,3 +84,13 @@ void linked_list_reverse_mutate(struct linked_list *list) {
(*current) = temporary_current; (*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);
}

View File

@ -62,4 +62,10 @@ struct linked_list *linked_list_reverse(struct linked_list *list);
*/ */
void linked_list_reverse_mutate(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 #endif

View File

@ -13,6 +13,7 @@ void linked_list_initialization_test() {
struct linked_list *list = linked_list_initialization(); struct linked_list *list = linked_list_initialization();
assert(list->length == 0); assert(list->length == 0);
assert(list->head == NULL); assert(list->head == NULL);
linked_list_free(list);
} }
void linked_list_add_in_head_test() { 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->data) == 8);
assert(((uintptr_t)list->head->next->next->data) == 4); assert(((uintptr_t)list->head->next->next->data) == 4);
assert(list->head->next->next->next == NULL); assert(list->head->next->next->next == NULL);
linked_list_free(list);
} }
void linked_list_delete_in_head_test() { 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->data) == 8);
assert(((uintptr_t)list->head->next->data) == 4); assert(((uintptr_t)list->head->next->data) == 4);
assert(list->head->next->next == NULL); assert(list->head->next->next == NULL);
linked_list_free(list);
} }
void linked_list_add_after_last_test() { 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->data) == 8);
assert(((uintptr_t)list->head->next->next->data) == 4); assert(((uintptr_t)list->head->next->next->data) == 4);
assert(((uintptr_t)list->head->next->next->next->data) == 18); assert(((uintptr_t)list->head->next->next->next->data) == 18);
linked_list_free(list);
} }
void linked_list_reverse_test() { 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->data) == (void *)'C');
assert((list_reversed->head->next->next->data) == (void *)'B'); assert((list_reversed->head->next->next->data) == (void *)'B');
assert((list_reversed->head->next->next->next->data) == (void *)'A'); assert((list_reversed->head->next->next->next->data) == (void *)'A');
linked_list_free(list);
} }
void linked_list_reverse_mutate_test() { 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->data) == (void *)'C');
assert((list->head->next->next->data) == (void *)'B'); assert((list->head->next->next->data) == (void *)'B');
assert((list->head->next->next->next->data) == (void *)'A'); assert((list->head->next->next->next->data) == (void *)'A');
linked_list_free(list);
} }