From 9d571c694eb129816b6de6509853c2bf30f95ba1 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 25 Apr 2025 15:18:44 -0500 Subject: [PATCH] feat(linked list): added append function --- Include/Fledasty/Core/LinkedList.h | 2 ++ Src/Core/LinkedList.c | 18 +++++++++++++++++- Tests/Main.c | 4 ++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 87bf47a..e482ed2 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -41,4 +41,6 @@ typedef struct { FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size); FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_list); +FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_list, void *value); + #endif diff --git a/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index a6e9c99..1b23852 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -85,4 +85,20 @@ FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_li } return FLEDASTY_ERROR_NONE; -} \ No newline at end of file +} + +FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_list, void *value) { + if (current_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + new_node->next = NULL; + + current_linked_list->end->next = new_node; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index a07db2c..bc8a4d2 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -106,6 +106,10 @@ int main() { FledastyLinkedList test_linked_list; fledasty_linked_list_initialize(&test_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); + for (int i = 0; i < 10; i += 1) { + fledasty_linked_list_append(&test_linked_list, &i); + } + fledasty_linked_list_destroy(&test_linked_list); printf("Done\n"); return 0;