f2-basic-stack #17

Merged
Mineplay merged 7 commits from f2-basic-stack into main 2025-04-24 04:51:22 -05:00
3 changed files with 15 additions and 3 deletions
Showing only changes of commit 01ce91cfa4 - Show all commits

View file

@ -38,6 +38,7 @@ 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);
static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }

View file

@ -76,3 +76,11 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) {
return FLEDASTY_ERROR_NONE;
}
void *fledasty_stack_peek(FledastyStack *current_stack) {
if (current_stack == NULL) {
return NULL;
}
return current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size);
}

View file

@ -31,8 +31,8 @@ int main() {
fledasty_queue_push(&test_queue, &i);
}
int *peek_data = (int*)fledasty_queue_peek(&test_queue);
printf("Peeked: %d\n", *peek_data);
int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue);
printf("Peeked: %d\n", *peeked_queue_data);
for (int i = test_queue.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
@ -52,6 +52,9 @@ int main() {
fledasty_stack_push(&test_stack, &i);
}
int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack);
printf("Peeked: %d\n", *peeked_stack_data);
if (fledasty_stack_is_empty(&test_stack)) {
printf("Stack is empty\n");
}