From 8b0a617cb80a59d3cfaf4396620dad024a2eb0d8 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Wed, 23 Apr 2025 12:05:36 -0500 Subject: [PATCH] feat(stack): implemented pop function for stack --- Include/Fledasty/Core/Queue.h | 2 +- Include/Fledasty/Core/Stack.h | 3 ++- Src/Core/Queue.c | 2 +- Src/Core/Stack.c | 13 ++++++++++++- Tests/Main.c | 5 +++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index a874f91..762262f 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -39,7 +39,7 @@ FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t e FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value); -void *fledasty_queue_peek(FledastyQueue *current_queue); +void *fledasty_queue_peek(const FledastyQueue *current_queue); void *fledasty_queue_pop(FledastyQueue *current_queue); static inline size_t fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; } diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index 7438ecf..bcf0a97 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -38,7 +38,8 @@ FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t e FledastyError fledasty_stack_destroy(FledastyStack *current_stack); FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value); -void *fledasty_stack_peek(FledastyStack *current_stack); +void *fledasty_stack_peek(const FledastyStack *current_stack); +void *fledasty_stack_pop(FledastyStack *current_stack); static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; } diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index e076b46..37d44c1 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -89,7 +89,7 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { return FLEDASTY_ERROR_NONE; } -void *fledasty_queue_peek(FledastyQueue *current_queue) { +void *fledasty_queue_peek(const FledastyQueue *current_queue) { if (current_queue == NULL) { return NULL; } diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index 71e3a11..4a377c0 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -77,10 +77,21 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) { return FLEDASTY_ERROR_NONE; } -void *fledasty_stack_peek(FledastyStack *current_stack) { +void *fledasty_stack_peek(const FledastyStack *current_stack) { if (current_stack == NULL) { return NULL; } return current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size); +} + +void *fledasty_stack_pop(FledastyStack *current_stack) { + if (current_stack == NULL) { + return NULL; + } + + void *value_address = current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size); + current_stack->size -= 1; + + return value_address; } \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index c0dca96..933fd43 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -55,6 +55,11 @@ int main() { int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack); printf("Peeked: %d\n", *peeked_stack_data); + for (int i = test_stack.size; i > 0; i -= 1) { + int *popped_data = (int*)fledasty_stack_pop(&test_stack); + printf("Popped: %d\n", *popped_data); + } + if (fledasty_stack_is_empty(&test_stack)) { printf("Stack is empty\n"); }