feat(stack): implemented pop function for stack

This commit is contained in:
Mineplay 2025-04-23 12:05:36 -05:00
parent 01ce91cfa4
commit 8b0a617cb8
5 changed files with 21 additions and 4 deletions

View file

@ -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; }

View file

@ -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; }

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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");
}