From 9fe693343fa3efd0caa34a5dda68326e1a75eee3 Mon Sep 17 00:00:00 2001 From: Luiz Felipe Date: Sun, 19 Jan 2025 15:06:51 -0300 Subject: [PATCH] feat: add stack implementation --- src/include/stack.h | 48 +++++++++++++++++++++++++++++++++++++++ src/include/types/stack.h | 14 ++++++++++++ src/types/stack.c | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/include/stack.h create mode 100644 src/include/types/stack.h create mode 100644 src/types/stack.c diff --git a/src/include/stack.h b/src/include/stack.h new file mode 100644 index 0000000..aa12188 --- /dev/null +++ b/src/include/stack.h @@ -0,0 +1,48 @@ +#pragma once + +#include + +#include "types/err.h" +#include "types/stack.h" + +/** + * Initializes the given stack. + * + * @param stack The stack to be initialized. + */ +void +stack_init(stack_t* stack); + +/** + * Push a new value on the stack. + * + * @param stack The stack where tbe value will be pushed. + * @param value The value to push on the stack. + * + * @return The index of the new value inside the stack. + */ +unsigned int +stack_push(stack_t* stack, void* value); + +/** + * Pop a value from the stack. + * + * @param stack The stack from where the value will be poped. + * @param value Pointer for where the poped value will be saved. + * + * @return ERR_EMPTY on stack is empty. + * @return ERR_OK on value is poped successful. + */ +error_code_t +stack_pop(stack_t* stack, void** value); + +/** + * Check if the given stack is empty. + * + * @param stack The stack to check. + * + * @return true on stack is empty. + * @return false if stack has values. + */ +bool +stack_isempty(stack_t* stack); diff --git a/src/include/types/stack.h b/src/include/types/stack.h new file mode 100644 index 0000000..875f32c --- /dev/null +++ b/src/include/types/stack.h @@ -0,0 +1,14 @@ +#pragma once + +#define STACK_INITIAL_LENGTH 20 +#define STACK_LENGTH_INCREMENT 50 + +/** + * A struct representing a dynamic stack. Each value on the stack is a `void *`. + */ +typedef struct stack +{ + void** values; ///< Array of values on the stack. + unsigned int length; ///< Number of elements on the stack. + unsigned int _size; ///< Current number of elements that fits on the stack. +} stack_t; diff --git a/src/types/stack.c b/src/types/stack.c new file mode 100644 index 0000000..2929fba --- /dev/null +++ b/src/types/stack.c @@ -0,0 +1,44 @@ +#include + +#include "stack.h" +#include "types/err.h" + +void +stack_init(stack_t* stack) +{ + stack->length = 0; + + stack->values = malloc(sizeof(void*) * STACK_INITIAL_LENGTH); + stack->_size = STACK_INITIAL_LENGTH; +} + +unsigned int +stack_push(stack_t* stack, void* value) +{ + if (stack->length >= stack->_size) { + stack->_size += STACK_LENGTH_INCREMENT; + stack->values = realloc(stack->values, sizeof(void*) * stack->_size); + } + + stack->values[stack->length] = value; + + return stack->length++; +} + +error_code_t +stack_pop(stack_t* stack, void** value) +{ + if (stack_isempty(stack)) { + return ERR_EMPTY; + } + + *value = stack->values[--stack->length]; + + return ERR_OK; +} + +bool +stack_isempty(stack_t* stack) +{ + return stack->length == 0; +}