From 0c306da31c5a38945f1ba577885dde45d23a6542 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sat, 26 Apr 2025 08:56:56 -0500 Subject: [PATCH 1/9] feat(doubly linked list): added doubly linked list datastructure and implemented init and destroy functions --- Include/Fledasty/Core/DoublyLinkedList.h | 45 +++++++++++ Include/Fledasty/Core/LinkedList.h | 2 +- Src/Core/DoublyLinkedList.c | 95 ++++++++++++++++++++++++ Tests/Main.c | 6 ++ 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 Include/Fledasty/Core/DoublyLinkedList.h create mode 100644 Src/Core/DoublyLinkedList.c diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h new file mode 100644 index 0000000..0ed8ab7 --- /dev/null +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -0,0 +1,45 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: DoublyLinkedList.c + * Description: + * This file contains the functions for modifying the Doubly Linked List. It + * includes functions to append, Insert before, Insert after, Insert at index, + * Get, Remove element, Remove at index, Check if has element and Check if is empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_DOUBLY_LINKED_LIST +#define FLEDASTY_DOUBLY_LINKED_LIST + +#include +#include + +#include "../Utils/Error.h" + +typedef struct FledastyDoublyLinkedListNode { + void *value; + struct FledastyDoublyLinkedListNode *previous, *next; +} FledastyDoublyLinkedListNode; + +typedef struct { + size_t size, element_byte_size; + FledastyDoublyLinkedListNode *start, *end; +} FledastyDoublyLinkedList; + +FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size); +FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_linked_list); + +#endif \ No newline at end of file diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 82c2fc6..718d3fe 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -12,7 +12,7 @@ * limitations under the License. * * ----------------------------------------------------------------------------- - * File: LinkedList.c + * File: LinkedList.h * Description: * This file contains the functions for modifying the Linked List. It includes * functions to append, Insert before, Insert after, Insert at index, diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c new file mode 100644 index 0000000..7ae719d --- /dev/null +++ b/Src/Core/DoublyLinkedList.c @@ -0,0 +1,95 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: DoublyLinkedList.c + * Description: + * This file contains the functions for modifying the Doubly Linked List. It + * includes functions to append, Insert before, Insert after, Insert at index, + * Get, Remove element, Remove at index, Check if has element and Check if is empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Core/DoublyLinkedList.h" + +#include +#include +#include + +FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size) { + if (new_linked_list == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + new_linked_list->element_byte_size = element_byte_size; + if (values == NULL || values_size == 0) { + new_linked_list->size = 0; + new_linked_list->start = NULL; + new_linked_list->end = NULL; + } else { + new_linked_list->size = values_size; + + new_linked_list->start = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode)); + new_linked_list->start->value = hallocy_malloc(new_linked_list->element_byte_size); + hallocy_copy_memory(new_linked_list->start->value, values, new_linked_list->element_byte_size); + + new_linked_list->start->previous = NULL; + new_linked_list->start->next = NULL; + + FledastyDoublyLinkedListNode *last_node = new_linked_list->start; + for (size_t index = new_linked_list->element_byte_size; index < new_linked_list->size * new_linked_list->element_byte_size; index += new_linked_list->element_byte_size) { + FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode)); + new_node->value = hallocy_malloc(new_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_linked_list->element_byte_size); + + new_node->previous = last_node; + new_node->next = NULL; + + last_node->next = new_node; + last_node = new_node; + } + + new_linked_list->end = last_node; + } + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_linked_list) { + if (current_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyDoublyLinkedListNode *current_node = current_linked_list->start; + while (current_node->next != NULL) { + current_node = current_node->next; + + HallocyError result = hallocy_free(current_node->previous->value); + if (result == HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + result = hallocy_free(current_node->previous); + if (result == HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + if (current_node != NULL) { + hallocy_free(current_node->value); + hallocy_free(current_node); + } + + return FLEDASTY_ERROR_NONE; +} \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index 4a7805e..b4539c1 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -24,6 +24,7 @@ #include #include #include +#include int main() { FledastyQueue test_queue; @@ -137,6 +138,11 @@ int main() { } fledasty_linked_list_destroy(&test_linked_list); + + FledastyDoublyLinkedList test_doubly_linked_list; + fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); + + fledasty_doubly_list_destroy(&test_doubly_linked_list); printf("Done\n"); return 0; } \ No newline at end of file From d0c552362beeda71c947c6b2bbd623ac091854a7 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sat, 26 Apr 2025 09:20:11 -0500 Subject: [PATCH 2/9] feat(doubly linked list): implemented append function --- Include/Fledasty/Core/DoublyLinkedList.h | 6 ++- Src/Core/DoublyLinkedList.c | 62 ++++++++++++++++-------- Tests/Main.c | 10 ++++ 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 0ed8ab7..b5b55d9 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -39,7 +39,9 @@ typedef struct { FledastyDoublyLinkedListNode *start, *end; } FledastyDoublyLinkedList; -FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size); -FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_linked_list); +FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size); +FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_doubly_linked_list); + +FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value); #endif \ No newline at end of file diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 7ae719d..50e7bf6 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -27,31 +27,31 @@ #include #include -FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_linked_list, void *values, const size_t values_size, const size_t element_byte_size) { - if (new_linked_list == NULL) { +FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size) { + if (new_doubly_linked_list == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - new_linked_list->element_byte_size = element_byte_size; + new_doubly_linked_list->element_byte_size = element_byte_size; if (values == NULL || values_size == 0) { - new_linked_list->size = 0; - new_linked_list->start = NULL; - new_linked_list->end = NULL; + new_doubly_linked_list->size = 0; + new_doubly_linked_list->start = NULL; + new_doubly_linked_list->end = NULL; } else { - new_linked_list->size = values_size; + new_doubly_linked_list->size = values_size; - new_linked_list->start = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode)); - new_linked_list->start->value = hallocy_malloc(new_linked_list->element_byte_size); - hallocy_copy_memory(new_linked_list->start->value, values, new_linked_list->element_byte_size); + new_doubly_linked_list->start = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode)); + new_doubly_linked_list->start->value = hallocy_malloc(new_doubly_linked_list->element_byte_size); + hallocy_copy_memory(new_doubly_linked_list->start->value, values, new_doubly_linked_list->element_byte_size); - new_linked_list->start->previous = NULL; - new_linked_list->start->next = NULL; + new_doubly_linked_list->start->previous = NULL; + new_doubly_linked_list->start->next = NULL; - FledastyDoublyLinkedListNode *last_node = new_linked_list->start; - for (size_t index = new_linked_list->element_byte_size; index < new_linked_list->size * new_linked_list->element_byte_size; index += new_linked_list->element_byte_size) { + FledastyDoublyLinkedListNode *last_node = new_doubly_linked_list->start; + for (size_t index = new_doubly_linked_list->element_byte_size; index < new_doubly_linked_list->size * new_doubly_linked_list->element_byte_size; index += new_doubly_linked_list->element_byte_size) { FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode)); - new_node->value = hallocy_malloc(new_linked_list->element_byte_size); - hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_linked_list->element_byte_size); + new_node->value = hallocy_malloc(new_doubly_linked_list->element_byte_size); + hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_doubly_linked_list->element_byte_size); new_node->previous = last_node; new_node->next = NULL; @@ -60,18 +60,18 @@ FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *n last_node = new_node; } - new_linked_list->end = last_node; + new_doubly_linked_list->end = last_node; } return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_linked_list) { - if (current_linked_list == NULL) { +FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_doubly_linked_list) { + if (current_doubly_linked_list == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - FledastyDoublyLinkedListNode *current_node = current_linked_list->start; + FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start; while (current_node->next != NULL) { current_node = current_node->next; @@ -92,4 +92,24 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_lin } return FLEDASTY_ERROR_NONE; -} \ No newline at end of file +} + +FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { + if (current_doubly_linked_list == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + 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); + + 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; + current_doubly_linked_list->size += 1; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index b4539c1..dead7ab 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -142,6 +142,16 @@ int main() { FledastyDoublyLinkedList test_doubly_linked_list; fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); + for (int i = 0; i < 10; i += 1) { + fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i); + } + + 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; + } + fledasty_doubly_list_destroy(&test_doubly_linked_list); printf("Done\n"); return 0; From b969e85412c8a616bbbf99cdd7406d205cb653fd Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 29 Apr 2025 16:02:11 -0500 Subject: [PATCH 3/9] 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; From e56ed3331fc098db86e96894b0878d2e5319bf08 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Tue, 29 Apr 2025 17:58:53 -0500 Subject: [PATCH 4/9] 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) { From 620ce5668cd44cdb1e11c699844e8d1548c31b54 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 30 Apr 2025 06:43:58 -0500 Subject: [PATCH 5/9] 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) { From 984f89388559d98063b276b33be1e047dae2210f Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 03:27:08 -0500 Subject: [PATCH 6/9] feat(doubly linked list): implemented is empty function --- Include/Fledasty/Core/DoublyLinkedList.h | 2 ++ Tests/Main.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index b080996..96f96ea 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -48,4 +48,6 @@ FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedLi 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); +static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; } + #endif \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index 091ce51..e75e1dd 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -166,6 +166,10 @@ int main() { test_doubly_linked_list_node = test_doubly_linked_list_node->previous; } + if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) { + printf("Linked list is empty\n"); + } + fledasty_doubly_list_destroy(&test_doubly_linked_list); printf("Done\n"); return 0; From bd6b4b1335f7d511b5626b9a493554985b97f9a5 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 04:25:49 -0500 Subject: [PATCH 7/9] feat(doubly linked list): implemented has value function --- Include/Fledasty/Core/DoublyLinkedList.h | 1 + Src/Core/DoublyLinkedList.c | 17 +++++++++++++++++ Tests/Main.c | 6 +++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 96f96ea..cc08229 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -48,6 +48,7 @@ FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedLi 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); +bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value); static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; } #endif \ No newline at end of file diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index f9c4705..5dfda41 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -239,3 +239,20 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke current_doubly_linked_list->size += 1; return FLEDASTY_ERROR_NONE; } + +bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { + if (current_doubly_linked_list == NULL || value == NULL) { + return false; + } + + FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start; + while (current_node != NULL) { + if (hallocy_compare_memory(current_node->value, value, current_doubly_linked_list->element_byte_size)) { + return true; + } + + current_node = current_node->next; + } + + return false; +} diff --git a/Tests/Main.c b/Tests/Main.c index e75e1dd..c051e36 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -166,8 +166,12 @@ int main() { test_doubly_linked_list_node = test_doubly_linked_list_node->previous; } + if (fledasty_linked_list_has_value(&test_linked_list, &insert_value)) { + printf("Doubly linked list contains %d\n", insert_value); + } + if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) { - printf("Linked list is empty\n"); + printf("Doubly linked list is empty\n"); } fledasty_doubly_list_destroy(&test_doubly_linked_list); From e2b88338a3a34f8d15e9d41096d7070b3860e619 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 05:26:50 -0500 Subject: [PATCH 8/9] feat(doubly linked list): implemented remove at index function --- Include/Fledasty/Core/DoublyLinkedList.h | 2 ++ Src/Core/DoublyLinkedList.c | 44 ++++++++++++++++++++++++ Tests/Main.c | 4 ++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index cc08229..073f5b2 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -48,6 +48,8 @@ FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedLi 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); +FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index); + bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value); static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; } diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 5dfda41..37085ec 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -240,6 +240,50 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index) { + if (current_doubly_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (index >= current_doubly_linked_list->size) { + return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE; + } + + 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; 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; node -= 1) { + current_node = current_node->previous; + } + } + + if (current_node->previous == NULL) { + current_doubly_linked_list->start = current_node->next; + current_doubly_linked_list->start->previous = NULL; + if (current_node->next == NULL) { + current_doubly_linked_list->end = current_node->previous; + } + } else { + current_node->previous->next = current_node->next; + if (current_node->next == NULL) { + current_doubly_linked_list->end = current_node->previous; + } else { + current_node->next->previous = current_node->previous; + } + } + + hallocy_free(current_node->value); + hallocy_free(current_node); + + current_doubly_linked_list->size -= 1; + return FLEDASTY_ERROR_NONE; +} + bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { if (current_doubly_linked_list == NULL || value == NULL) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index c051e36..755c5cf 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -154,6 +154,8 @@ int main() { insert_value = 90; fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value); + fledasty_doubly_linked_list_remove_at_index(&test_doubly_linked_list, 2); + 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); @@ -166,7 +168,7 @@ int main() { test_doubly_linked_list_node = test_doubly_linked_list_node->previous; } - if (fledasty_linked_list_has_value(&test_linked_list, &insert_value)) { + if (fledasty_doubly_linked_list_has_value(&test_doubly_linked_list, &insert_value)) { printf("Doubly linked list contains %d\n", insert_value); } From 19c9436ae0276bd87465c2b8d2e70daee703dd0a Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 05:36:56 -0500 Subject: [PATCH 9/9] feat(doubly linked list): implemented remove value function --- Include/Fledasty/Core/DoublyLinkedList.h | 1 + Src/Core/DoublyLinkedList.c | 36 ++++++++++++++++++++++++ Src/Core/LinkedList.c | 1 - Tests/Main.c | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 073f5b2..5a0575b 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -49,6 +49,7 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *after_value, void *value); FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index); +FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value); bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value); static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; } diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 37085ec..c1bdb2f 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -284,6 +284,42 @@ FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedLi return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { + if (current_doubly_linked_list == 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, value, current_doubly_linked_list->element_byte_size)) { + current_node = current_node->next; + } + + if (current_node == NULL) { + return FLEDASTY_ERROR_VALUE_NOT_FOUND; + } + + if (current_node->previous == NULL) { + current_doubly_linked_list->start = current_node->next; + current_doubly_linked_list->start->previous = NULL; + if (current_node->next == NULL) { + current_doubly_linked_list->end = current_node->previous; + } + } else { + current_node->previous->next = current_node->next; + if (current_node->next == NULL) { + current_doubly_linked_list->end = current_node->previous; + } else { + current_node->next->previous = current_node->previous; + } + } + + hallocy_free(current_node->next); + hallocy_free(current_node); + + current_doubly_linked_list->size -= 1; + return FLEDASTY_ERROR_NONE; +} + bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { if (current_doubly_linked_list == NULL || value == NULL) { return false; diff --git a/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index 2e3a32f..b13864c 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -268,7 +268,6 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link hallocy_free(current_node->next); hallocy_free(current_node); - current_linked_list->size -= 1; return FLEDASTY_ERROR_NONE; } diff --git a/Tests/Main.c b/Tests/Main.c index 755c5cf..f474731 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -155,6 +155,8 @@ int main() { fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value); fledasty_doubly_linked_list_remove_at_index(&test_doubly_linked_list, 2); + remove_value = 0; + fledasty_doubly_linked_list_remove_value(&test_doubly_linked_list, &remove_value); FledastyDoublyLinkedListNode *test_doubly_linked_list_node = test_doubly_linked_list.start; for (int i = 0; i < test_doubly_linked_list.size; i += 1) {