From 620ce5668cd44cdb1e11c699844e8d1548c31b54 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 30 Apr 2025 06:43:58 -0500 Subject: [PATCH] feat(doubly linked list): implemented insert after value function --- Include/Fledasty/Core/DoublyLinkedList.h | 1 + Src/Core/DoublyLinkedList.c | 37 ++++++++++++++++++++++++ Tests/Main.c | 2 ++ 3 files changed, 40 insertions(+) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 9370811..b080996 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -46,5 +46,6 @@ FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *curre FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value); 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); #endif \ No newline at end of file diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index ee6c0a0..f9c4705 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -202,3 +202,40 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink current_doubly_linked_list->size += 1; return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value) { + if (current_doubly_linked_list == NULL || after_value == 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, after_value, current_doubly_linked_list->element_byte_size)) { + current_node = current_node->next; + } + + if (current_node == NULL) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode)); + + new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size); + + if (current_node == current_doubly_linked_list->end) { + new_node->next = NULL; + new_node->previous = current_doubly_linked_list->end; + + current_doubly_linked_list->end->next = new_node; + current_doubly_linked_list->end = new_node; + } else { + new_node->previous = current_node; + new_node->next = current_node->next; + + current_node->next->previous = new_node; + current_node->next = new_node; + } + + current_doubly_linked_list->size += 1; + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index abb68d2..091ce51 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -151,6 +151,8 @@ int main() { insert_value = 28; insert_at_value = 35; fledasty_doubly_linked_list_insert_before_value(&test_doubly_linked_list, &insert_at_value, &insert_value); + insert_value = 90; + fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value); FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start; for (int i = 0; i < test_doubly_linked_list.size; i += 1) {