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