From 8a8909ab7f53a69faa122afe4723e444573a84a9 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 2 May 2025 06:13:04 -0500 Subject: [PATCH] feat(linked list): implemented clear function --- Include/Fledasty/Core/HashTable.h | 6 ++--- Include/Fledasty/Core/LinkedList.h | 2 ++ Src/Core/DoublyLinkedList.c | 6 ++--- Src/Core/HashTable.c | 4 ++-- Src/Core/LinkedList.c | 35 +++++++++++++++++++++++++++--- Tests/Main.c | 4 ++-- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 5cf270b..1ccd35b 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -47,12 +47,12 @@ FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table); FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value); -void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key); +void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key); FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key); FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table); -bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key); -static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; } +bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key); +static inline bool fledasty_hash_table_is_empty(const FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; } #endif \ No newline at end of file diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 7089340..7505a73 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -51,6 +51,8 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index); FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_linked_list, void *value); +FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list); + bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value); static inline bool fledasty_linked_list_is_empty(const FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; } diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 62bf88d..967bb1c 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -76,12 +76,12 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou current_node = current_node->next; HallocyError result = hallocy_free(current_node->previous->value); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } result = hallocy_free(current_node->previous); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } @@ -312,7 +312,7 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList } } - hallocy_free(current_node->next); + hallocy_free(current_node->value); hallocy_free(current_node); current_doubly_linked_list->size -= 1; diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index 67f90ef..8e92a31 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -107,7 +107,7 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, return FLEDASTY_ERROR_NONE; } -void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) { +void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key) { if (current_hash_table == NULL || key == NULL) { return NULL; } @@ -181,7 +181,7 @@ FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) { return FLEDASTY_ERROR_NONE; } -bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key) { +bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key) { if (current_hash_table == NULL || key == NULL) { return false; } diff --git a/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index b13864c..76d90e1 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -74,12 +74,12 @@ FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_li current_node = current_node->next; HallocyError result = hallocy_free(previous_node->value); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } result = hallocy_free(previous_node); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } @@ -265,13 +265,42 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link previous_node->next = current_node->next; } - hallocy_free(current_node->next); + hallocy_free(current_node->value); hallocy_free(current_node); current_linked_list->size -= 1; return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list) { + if (current_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_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_linked_list->size = 0; + current_linked_list->start = NULL; + current_linked_list->end = NULL; + + return FLEDASTY_ERROR_NONE; +} + bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value) { if (current_linked_list == NULL || value == NULL) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index 3489d3b..2404d84 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -139,15 +139,15 @@ int main() { printf("Linked list contains %d\n", insert_value); } + fledasty_linked_list_clear(&test_linked_list); if (fledasty_linked_list_is_empty(&test_linked_list)) { printf("Linked list is empty\n"); } fledasty_linked_list_destroy(&test_linked_list); - + FledastyDoublyLinkedList test_doubly_linked_list; fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); - for (int i = 0; i < 10; i += 1) { fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i); }