diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 7013824..c588131 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -52,6 +52,8 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index); FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value); +FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list); + bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value); static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; } diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 967bb1c..0c06d23 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -71,26 +71,23 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou return FLEDASTY_ERROR_INVALID_POINTER; } + FledastyDoublyLinkedListNode *previous_node = NULL; FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start; - while (current_node->next != NULL) { + while (current_node != NULL) { + previous_node = current_node; current_node = current_node->next; - HallocyError result = hallocy_free(current_node->previous->value); + HallocyError result = hallocy_free(previous_node->value); if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - result = hallocy_free(current_node->previous); + result = hallocy_free(previous_node); if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } - if (current_node != NULL) { - hallocy_free(current_node->value); - hallocy_free(current_node); - } - return FLEDASTY_ERROR_NONE; } @@ -319,6 +316,35 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list) { + if (current_doubly_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyDoublyLinkedListNode *previous_node = NULL; + FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start; + while (current_node != NULL) { + previous_node = current_node; + current_node = current_node->next; + + HallocyError result = hallocy_free(previous_node->value); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + result = hallocy_free(previous_node); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + current_doubly_linked_list->size = 0; + current_doubly_linked_list->start = NULL; + current_doubly_linked_list->end = NULL; + + return FLEDASTY_ERROR_NONE; +} + bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { if (current_doubly_linked_list == NULL || value == NULL) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index 2404d84..f0cd693 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -180,6 +180,7 @@ int main() { printf("Doubly linked list contains %d\n", insert_value); } + fledasty_doubly_linked_list_clear(&test_doubly_linked_list); if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) { printf("Doubly linked list is empty\n"); }