From b969e85412c8a616bbbf99cdd7406d205cb653fd Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 29 Apr 2025 16:02:11 -0500 Subject: [PATCH] feat(doubly linked list): implemented insert at index --- Include/Fledasty/Core/DoublyLinkedList.h | 2 + Src/Core/DoublyLinkedList.c | 51 ++++++++++++++++++++++++ Src/Core/LinkedList.c | 1 + Tests/Main.c | 9 +++++ 4 files changed, 63 insertions(+) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index b5b55d9..0b754fa 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -44,4 +44,6 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value); +FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value); + #endif \ No newline at end of file diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 50e7bf6..db9bd1d 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -113,3 +113,54 @@ FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *curre return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index, void *value) { + if (current_doubly_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (index > current_doubly_linked_list->size) { + return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + } + + 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 (index == 0) { + new_node->previous = NULL; + new_node->next = current_doubly_linked_list->start; + + current_doubly_linked_list->start->previous = new_node; + current_doubly_linked_list->start = new_node; + } else if (index == current_doubly_linked_list->size) { + new_node->previous = current_doubly_linked_list->end; + new_node->next = NULL; + + current_doubly_linked_list->end->next = new_node; + current_doubly_linked_list->end = new_node; + } else { + 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 - 1; 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 - 1; node -= 1) { + current_node = current_node->previous; + } + } + + 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/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index 5b415e1..2e3a32f 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -123,6 +123,7 @@ FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_l new_node->next = current_linked_list->start; current_linked_list->start = new_node; } else if (index == current_linked_list->size) { + new_node->next = NULL; current_linked_list->end->next = new_node; current_linked_list->end = new_node; } else { diff --git a/Tests/Main.c b/Tests/Main.c index dead7ab..679cbb6 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -146,12 +146,21 @@ int main() { fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i); } + insert_value = 35; + fledasty_doubly_linked_list_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, &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) { printf("Doubly linked list get: %d\n", *(int*)test_doubly_linked_list_node->value); test_doubly_linked_list_node = test_doubly_linked_list_node->next; } + test_doubly_linked_list_node = test_doubly_linked_list.end; + for (int i = 0; i < test_doubly_linked_list.size; i += 1) { + printf("Doubly linked list get backwards: %d\n", *(int*)test_doubly_linked_list_node->value); + test_doubly_linked_list_node = test_doubly_linked_list_node->previous; + } + fledasty_doubly_list_destroy(&test_doubly_linked_list); printf("Done\n"); return 0;