feat(doubly linked list): added insert before value function

This commit is contained in:
Mineplay 2025-04-29 17:58:53 -05:00
parent b969e85412
commit e56ed3331f
3 changed files with 42 additions and 0 deletions

View file

@ -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

View file

@ -22,6 +22,7 @@
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/DoublyLinkedList.h"
#include "Fledasty/Utils/Error.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
@ -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;
}

View file

@ -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) {