f4-basic-linked-list #19

Merged
Mineplay merged 13 commits from f4-basic-linked-list into main 2025-04-26 07:41:38 -05:00
23 changed files with 151 additions and 12 deletions
Showing only changes of commit ffdb897cda - Show all commits

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

1
.gitignore vendored
View file

@ -1 +1,2 @@
Build
compile_commands.json

View file

@ -21,13 +21,13 @@
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef FLEDASTY_DYNAMIC_ARRAY
#define FLEDASTY_DYNAMIC_ARRAY
#ifndef FLEDASTY_DYNAMIC_ARRAY
#define FLEDASTY_DYNAMIC_ARRAY
#include <stddef.h>
#include <stddef.h>
#include <stdbool.h>
#include "../Utils/Error.h"
#include "../Utils/Error.h"
typedef struct {
size_t size, capacity;

View file

@ -0,0 +1,44 @@
/*
* 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: LinkedList.c
* Description:
* This file contains the functions for modifying the 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_LINKED_LIST
#define FLEDASTY_LINKED_LIST
#include <stddef.h>
#include "../Utils/Error.h"
typedef struct FledastyLinkedListNode{
void *value;
struct FledastyLinkedListNode *next;
} FledastyLinkedListNode;
typedef struct {
size_t size, element_byte_size;
FledastyLinkedListNode *start, *end;
} FledastyLinkedList;
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);
#endif

View file

@ -192,7 +192,7 @@ FledastyError fledasty_dynamic_array_remove_at_index(FledastyDynamicArray *curre
return FLEDASTY_ERROR_INDEX_OUT_OF_RANGE;
}
void *index_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
unsigned char *index_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
hallocy_copy_memory(index_pointer, index_pointer + current_dynamic_array->element_byte_size, (current_dynamic_array->size + index) * current_dynamic_array->element_byte_size);
current_dynamic_array->size -= 1;

88
Src/Core/LinkedList.c Normal file
View file

@ -0,0 +1,88 @@
/*
* 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: LinkedList.c
* Description:
* This file contains the functions for modifying the 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/LinkedList.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_linked_list_initialize(FledastyLinkedList *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 = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
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->next = NULL;
FledastyLinkedListNode *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) {
FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode));
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->next = NULL;
last_node->next = new_node;
last_node = new_node;
}
new_linked_list->end = last_node;
}
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_list) {
if (current_linked_list == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyLinkedListNode *previous_node = NULL;
FledastyLinkedListNode *current_node = current_linked_list->start;
while (current_node != NULL) {
previous_node = current_node;
current_node = current_node->next;
HallocyError result = hallocy_free(previous_node->value);
if (result == HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
result = hallocy_free(previous_node);
if (result == HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
return FLEDASTY_ERROR_NONE;
}

View file

@ -23,6 +23,7 @@
#include <Fledasty/Core/Queue.h>
#include <Fledasty/Core/Stack.h>
#include <Fledasty/Core/DynamicArray.h>
#include <Fledasty/Core/LinkedList.h>
int main() {
FledastyQueue test_queue;
@ -101,6 +102,11 @@ int main() {
}
fledasty_dynamic_array_destroy(&test_dynamic_array);
FledastyLinkedList test_linked_list;
fledasty_linked_list_initialize(&test_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
fledasty_linked_list_destroy(&test_linked_list);
printf("Done\n");
return 0;
}