From c7b866ce1d011311bc254c882e8dc753b78f6092 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sat, 26 Apr 2025 07:32:29 -0500 Subject: [PATCH] feat(linked list): implemented remove value --- Include/Fledasty/Core/LinkedList.h | 3 +- Src/Core/LinkedList.c | 73 +++++++++++++++++++++--------- Tests/Main.c | 2 + 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 09f8e45..c3ccc69 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -48,7 +48,8 @@ FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_l FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *current_linked_list, void *before_value, void *value); FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *current_linked_list, void *after_value, void *value); -FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_dynamic_array, const size_t index); +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); bool fledasty_linked_list_has_value(FledastyLinkedList *current_linked_list, void *value); static inline bool fledasty_linked_list_is_empty(FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; } diff --git a/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index ed840d8..fa6c8e2 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -215,29 +215,60 @@ FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_l return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; } - if (index == 0) { - FledastyLinkedListNode *remove_node = current_linked_list->start; - current_linked_list->start = current_linked_list->start->next; - - hallocy_free(remove_node->value); - hallocy_free(remove_node); - } else { - FledastyLinkedListNode *current_node = current_linked_list->start; - for (size_t node = 0; node < index - 1; node += 1) { - current_node = current_node->next; - } - - FledastyLinkedListNode *remove_node = current_node->next; - current_node->next = remove_node->next; - - hallocy_free(remove_node->value); - hallocy_free(remove_node); - - if (index == current_linked_list->size - 1) { - current_linked_list->end = current_node; - } + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_linked_list->start; + for (size_t node = 0; node < index; node += 1) { + previous_node = current_node; + current_node = current_node->next; } + if (current_node->next == NULL) { + current_linked_list->end = previous_node; + } + + if (previous_node == NULL) { + current_linked_list->start = current_node->next; + } else { + previous_node->next = 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_remove_value(FledastyLinkedList *current_linked_list, void *value) { + if (current_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_linked_list->start; + while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_linked_list->element_byte_size)) { + previous_node = current_node; + current_node = current_node->next; + } + + if (current_node == NULL) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + if (current_node->next == NULL) { + current_linked_list->end = previous_node; + } + + if (previous_node == NULL) { + current_linked_list->start = current_node->next; + } else { + previous_node->next = current_node->next; + } + + hallocy_free(current_node->next); + hallocy_free(current_node); + + current_linked_list->size -= 1; return FLEDASTY_ERROR_NONE; } diff --git a/Tests/Main.c b/Tests/Main.c index 8115263..4a7805e 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -119,6 +119,8 @@ int main() { fledasty_linked_list_insert_after_value(&test_linked_list, &insert_at_value, &insert_value); fledasty_linked_list_remove_at_index(&test_linked_list, 2); + remove_value = 0; + fledasty_linked_list_remove_value(&test_linked_list, &remove_value); FledastyLinkedListNode *test_linked_list_node = test_linked_list.start; for (int i = 0; i < test_linked_list.size; i += 1) {