feat(stack): added push function to stack

This commit is contained in:
Mineplay 2025-04-23 05:33:10 -05:00
parent 47f7db53bb
commit 0e430f3dd5
3 changed files with 26 additions and 0 deletions

View file

@ -37,6 +37,8 @@ typedef struct {
FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t element_byte_size);
FledastyError fledasty_stack_destroy(FledastyStack *current_stack);
FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value);
static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
#endif

View file

@ -54,5 +54,25 @@ FledastyError fledasty_stack_destroy(FledastyStack *current_stack) {
}
current_stack->buffer = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) {
if (current_stack == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (current_stack->size == current_stack->capacity) {
current_stack->capacity = current_stack->capacity + (current_stack->capacity / 2);
current_stack->buffer = (unsigned char*)hallocy_realloc(current_stack->buffer, current_stack->capacity);
if (current_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_copy_memory(current_stack->buffer + (current_stack->size * current_stack->element_byte_size), value, current_stack->element_byte_size);
current_stack->size += 1;
return FLEDASTY_ERROR_NONE;
}

View file

@ -48,6 +48,10 @@ int main() {
FledastyStack test_stack;
fledasty_stack_initialize(&test_stack, sizeof(int));
for (int i = 0; i < 10; i += 1) {
fledasty_stack_push(&test_stack, &i);
}
if (fledasty_stack_is_empty(&test_stack)) {
printf("Stack is empty\n");
}