diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index cc08229..073f5b2 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -48,6 +48,8 @@ FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedLi FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value); 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); + 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 5dfda41..37085ec 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -240,6 +240,50 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index) { + if (current_doubly_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (index >= current_doubly_linked_list->size) { + return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + } + + FledastyDoublyLinkedListNode *current_node = NULL; + if (index < current_doubly_linked_list->size / 2) { + current_node = current_doubly_linked_list->start; + for (size_t node = 0; node < index; node += 1) { + current_node = current_node->next; + } + } else { + current_node = current_doubly_linked_list->end; + for (size_t node = current_doubly_linked_list->size - 1; node > index; node -= 1) { + current_node = current_node->previous; + } + } + + 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->value); + 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/Tests/Main.c b/Tests/Main.c index c051e36..755c5cf 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -154,6 +154,8 @@ int main() { insert_value = 90; 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); + FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start; for (int i = 0; i < test_doubly_linked_list.size; i += 1) { printf("Doubly linked list get: %d\n", *(int*)test_doubly_linked_list_node->value); @@ -166,7 +168,7 @@ int main() { test_doubly_linked_list_node = test_doubly_linked_list_node->previous; } - if (fledasty_linked_list_has_value(&test_linked_list, &insert_value)) { + if (fledasty_doubly_linked_list_has_value(&test_doubly_linked_list, &insert_value)) { printf("Doubly linked list contains %d\n", insert_value); }