diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 073f5b2..5a0575b 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -49,6 +49,7 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value); 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); 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 37085ec..c1bdb2f 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -284,6 +284,42 @@ FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedLi return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { + if (current_doubly_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start; + while (current_node != NULL && !hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) { + current_node = current_node->next; + } + + if (current_node == NULL) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + if (current_node->previous == NULL) { + current_doubly_linked_list->start = current_node->next; + current_doubly_linked_list->start->previous = NULL; + if (current_node->next == NULL) { + current_doubly_linked_list->end = current_node->previous; + } + } else { + current_node->previous->next = current_node->next; + if (current_node->next == NULL) { + current_doubly_linked_list->end = current_node->previous; + } else { + current_node->next->previous = current_node->previous; + } + } + + hallocy_free(current_node->next); + hallocy_free(current_node); + + current_doubly_linked_list->size -= 1; + 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/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index 2e3a32f..b13864c 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -268,7 +268,6 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link 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 755c5cf..f474731 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -155,6 +155,8 @@ int main() { fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value); fledasty_doubly_linked_list_remove_at_index(&test_doubly_linked_list, 2); + remove_value = 0; + fledasty_doubly_linked_list_remove_value(&test_doubly_linked_list, &remove_value); FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start; for (int i = 0; i < test_doubly_linked_list.size; i += 1) {