f2-basic-stack #17
5 changed files with 21 additions and 4 deletions
|
|
@ -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_destroy(FledastyQueue *current_queue);
|
||||||
|
|
||||||
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
|
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);
|
void *fledasty_queue_pop(FledastyQueue *current_queue);
|
||||||
|
|
||||||
static inline size_t fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
|
static inline size_t fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
|
||||||
|
|
|
||||||
|
|
@ -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_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);
|
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; }
|
static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
|
||||||
return FLEDASTY_ERROR_NONE;
|
return FLEDASTY_ERROR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *fledasty_queue_peek(FledastyQueue *current_queue) {
|
void *fledasty_queue_peek(const FledastyQueue *current_queue) {
|
||||||
if (current_queue == NULL) {
|
if (current_queue == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,10 +77,21 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) {
|
||||||
return FLEDASTY_ERROR_NONE;
|
return FLEDASTY_ERROR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *fledasty_stack_peek(FledastyStack *current_stack) {
|
void *fledasty_stack_peek(const FledastyStack *current_stack) {
|
||||||
if (current_stack == NULL) {
|
if (current_stack == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size);
|
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;
|
||||||
}
|
}
|
||||||
|
|
@ -55,6 +55,11 @@ int main() {
|
||||||
int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack);
|
int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack);
|
||||||
printf("Peeked: %d\n", *peeked_stack_data);
|
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)) {
|
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