diff --git a/include/arc/std/queue.h b/include/arc/std/queue.h index e69de29..e833c48 100644 --- a/include/arc/std/queue.h +++ b/include/arc/std/queue.h @@ -0,0 +1,57 @@ +#ifndef ARC_STD_QUEUE_H_ +#define ARC_STD_QUEUE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief a queue type + */ +typedef struct ARC_Queue ARC_Queue; + +/** + * @brief creates ARC_Queue type + * + * @param queue ARC_Queue to initialize + */ +void ARC_Queue_Create(ARC_Queue **queue); + +/** + * @brief destroyes ARC_Queue type + */ +void ARC_Queue_Destroy(ARC_Queue *queue); + +/** + * @brief pushes value to end of queue + * + * @param queue ARC_Queue to push to + * @param data data that is being pushed + */ +void ARC_Queue_Push(ARC_Queue *queue, void *data); + +/** + * @brief pops the front off of the ARC_Queue + * + * @param queue ARC_Queue to remove from + * + * @return the poped data from the queue as a void * + */ +void *ARC_Queue_Pop(ARC_Queue *queue); + +/** + * @brief gets size of queue + * + * @param vector ARC_Queue to get size from + * + * @return the queues size + */ +uint32_t ARC_Queue_Size(ARC_Queue *queue); + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_QUEUE_H_ \ No newline at end of file diff --git a/include/arc/std/stack.h b/include/arc/std/stack.h index b4a0f90..daf1496 100644 --- a/include/arc/std/stack.h +++ b/include/arc/std/stack.h @@ -42,9 +42,9 @@ void ARC_Stack_Push(ARC_Stack *stack, void *data); void *ARC_Stack_Pop(ARC_Stack *stack); /** - * @brief gets size of vector + * @brief gets size of stack * - * @param vector ARC_Vector to get size from + * @param stack ARC_Stack to get size from * * @return the stacks size */ @@ -54,4 +54,4 @@ uint32_t ARC_Stack_Size(ARC_Stack *stack); } #endif -#endif //ARC_STD_STACK_H_ +#endif //ARC_STD_STACK_H_ \ No newline at end of file diff --git a/src/std/queue.c b/src/std/queue.c index e69de29..2baaec4 100644 --- a/src/std/queue.c +++ b/src/std/queue.c @@ -0,0 +1,76 @@ +#include "arc/std/queue.h" + +#include "arc/std/errno.h" +#include + +typedef struct ARC_QueueNode ARC_QueueNode; + +struct ARC_QueueNode { + ARC_QueueNode *next; + void *data; +}; + +struct ARC_Queue { + uint32_t currentSize; + ARC_QueueNode *node; +}; + +void ARC_Queue_Create(ARC_Queue **queue){ + *queue = (ARC_Queue *)malloc(sizeof(ARC_Queue)); + (*queue)->currentSize = 0; + (*queue)->node = NULL; +} + +void ARC_Queue_Destroy(ARC_Queue *queue){ + if(queue->currentSize != 0 || queue->node != NULL){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Queue_Destroy(queue) called, but queue was not empty"); + return; + } + + free(queue); +} + +void ARC_Queue_Push(ARC_Queue *queue, void *data){ + //TODO: check if size exceeds uint32_t + ARC_QueueNode *end = queue->node; + for(uint32_t i = 0; i < queue->currentSize; i++){ + //TODO: check if end->next exists + end = end->next; + } + + ARC_QueueNode *node = (ARC_QueueNode *)malloc(sizeof(ARC_QueueNode)); + node->data = data; + node->next = NULL; + + end->next = node; + + queue->currentSize++; +} + +void *ARC_Queue_Pop(ARC_Queue *queue){ + if(queue->currentSize == 0){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but queue was not empty"); + return NULL; + } + + ARC_QueueNode *node = queue->node; + if(node == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but node was NULL"); + return NULL; + } + + queue->node = node->next; + void *nodeData = node->data; + free(node); + + queue->currentSize--; + + return nodeData; +} + +uint32_t ARC_Queue_Size(ARC_Queue *queue){ + return queue->currentSize; +} \ No newline at end of file diff --git a/src/std/stack.c b/src/std/stack.c index 2593126..0cbed3a 100644 --- a/src/std/stack.c +++ b/src/std/stack.c @@ -24,7 +24,7 @@ void ARC_Stack_Create(ARC_Stack **stack){ void ARC_Stack_Destroy(ARC_Stack *stack){ if(stack->currentSize != 0 || stack->node != NULL){ arc_errno = ARC_ERRNO_DATA; - ARC_DEBUG_ERR("ARC_Stack_Destroy called, but stack was not empty"); + ARC_DEBUG_ERR("ARC_Stack_Destroy(stack) called, but stack was not empty"); return; } @@ -32,6 +32,7 @@ void ARC_Stack_Destroy(ARC_Stack *stack){ } void ARC_Stack_Push(ARC_Stack *stack, void *data){ + //TODO: check if size exceeds uint32_t ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode)); node->data = data; node->next = NULL; @@ -45,14 +46,14 @@ void ARC_Stack_Push(ARC_Stack *stack, void *data){ void *ARC_Stack_Pop(ARC_Stack *stack){ if(stack->currentSize == 0){ arc_errno = ARC_ERRNO_DATA; - ARC_DEBUG_ERR("ARC_Stack_Pop called, but stack was not empty"); + ARC_DEBUG_ERR("ARC_Stack_Pop(stack) called, but stack was not empty"); return NULL; } ARC_StackNode *node = stack->node; if(node == NULL){ arc_errno = ARC_ERRNO_NULL; - ARC_DEBUG_ERR("ARC_Stack_Pop called, but node was NULL"); + ARC_DEBUG_ERR("ARC_Stack_Pop(stack) called, but node was NULL"); return NULL; }