From e56ed3331fc098db86e96894b0878d2e5319bf08 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 29 Apr 2025 17:58:53 -0500 Subject: [PATCH] feat(doubly linked list): added insert before value function --- Include/Fledasty/Core/DoublyLinkedList.h | 1 + Src/Core/DoublyLinkedList.c | 38 ++++++++++++++++++++++++ Tests/Main.c | 3 ++ 3 files changed, 42 insertions(+) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 0b754fa..9370811 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -45,5 +45,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); +FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value); #endif \ No newline at end of file diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index db9bd1d..ee6c0a0 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -22,6 +22,7 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/DoublyLinkedList.h" +#include "Fledasty/Utils/Error.h" #include #include @@ -164,3 +165,40 @@ FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedLi current_doubly_linked_list->size += 1; return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *before_value, void *value) { + if (current_doubly_linked_list == NULL || before_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, before_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->previous == NULL) { + 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 { + new_node->previous = current_node->previous; + new_node->next = current_node; + + current_node->previous->next = new_node; + current_node->previous = new_node; + } + + current_doubly_linked_list->size += 1; + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index 679cbb6..abb68d2 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -148,6 +148,9 @@ int main() { insert_value = 35; fledasty_doubly_linked_list_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, &insert_value); + insert_value = 28; + insert_at_value = 35; + fledasty_doubly_linked_list_insert_before_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) {