feat(queue): added push function to queue

This commit is contained in:
Mineplay 2025-04-20 09:08:59 -05:00
parent 3e04e24b1c
commit 04744bb890
4 changed files with 48 additions and 3 deletions

View file

@ -40,4 +40,6 @@ typedef struct {
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue);
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue);
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size);
#endif

View file

@ -25,7 +25,7 @@
typedef enum {
FLEDASTY_ERROR_NONE = 0,
FLEDASTY_ERROR_ALLOCATION = 1,
FLEDASTY_ERROR_FAILED_ALLOCATION = 1,
FLEDASTY_ERROR_INVALID_POINTER = 2,
} FledastyError;

View file

@ -23,6 +23,7 @@
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Utils/Error.h>
#include <string.h>
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue) {
if (new_queue == NULL) {
@ -49,12 +50,12 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
HallocyError error = hallocy_free(previousNode->value);
if (error != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_ALLOCATION;
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
error = hallocy_free(previousNode);
if (error != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_ALLOCATION;
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
@ -62,5 +63,35 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
current_queue->start = NULL;
current_queue->end = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size) {
if (current_queue == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
FledastyQueueNode *new_queue_node = (FledastyQueueNode*)hallocy_malloc(sizeof(FledastyQueueNode));
if (new_queue_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_queue_node->value = (FledastyQueueNode*)hallocy_malloc(size);
hallocy_copy_memory(new_queue_node->value, value, size);
new_queue_node->next = NULL;
if (current_queue->start == NULL) {
current_queue->start = new_queue_node;
current_queue->end = new_queue_node;
new_queue_node->previous = NULL;
} else {
new_queue_node->previous = current_queue->end;
current_queue->end->next = new_queue_node;
current_queue->end = new_queue_node;
}
current_queue->size += 1;
return FLEDASTY_ERROR_NONE;
}

View file

@ -25,6 +25,18 @@
int main() {
FledastyQueue test_queue;
fledasty_queue_initialize(&test_queue);
int value = 10;
for (int i = 0; i < 10; i += 1) {
fledasty_queue_push(&test_queue, &i, sizeof(int));
}
FledastyQueueNode *current_node = test_queue.start;
for (int i = 0; i < test_queue.size; i++) {
printf("Element %d\n", *((int*)current_node->value));
current_node = current_node->next;
}
fledasty_queue_destroy(&test_queue);
printf("Done\n");