f2-basic-stack #17
3 changed files with 15 additions and 3 deletions
|
|
@ -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_destroy(FledastyStack *current_stack);
|
||||||
|
|
||||||
FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value);
|
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; }
|
static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,14 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) {
|
||||||
|
|
||||||
hallocy_copy_memory(current_stack->buffer + (current_stack->size * current_stack->element_byte_size), value, current_stack->element_byte_size);
|
hallocy_copy_memory(current_stack->buffer + (current_stack->size * current_stack->element_byte_size), value, current_stack->element_byte_size);
|
||||||
current_stack->size += 1;
|
current_stack->size += 1;
|
||||||
|
|
||||||
return FLEDASTY_ERROR_NONE;
|
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);
|
||||||
}
|
}
|
||||||
|
|
@ -31,8 +31,8 @@ int main() {
|
||||||
fledasty_queue_push(&test_queue, &i);
|
fledasty_queue_push(&test_queue, &i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int *peek_data = (int*)fledasty_queue_peek(&test_queue);
|
int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue);
|
||||||
printf("Peeked: %d\n", *peek_data);
|
printf("Peeked: %d\n", *peeked_queue_data);
|
||||||
|
|
||||||
for (int i = test_queue.size; i > 0; i -= 1) {
|
for (int i = test_queue.size; i > 0; i -= 1) {
|
||||||
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
|
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
|
||||||
|
|
@ -52,6 +52,9 @@ int main() {
|
||||||
fledasty_stack_push(&test_stack, &i);
|
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)) {
|
if (fledasty_stack_is_empty(&test_stack)) {
|
||||||
printf("Stack is empty\n");
|
printf("Stack is empty\n");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue