diff --git a/stack.c b/stack.c index 4699c297f6e1c6cb98bf59576eee848c6dd5b544..df4e9610c7926a25f16c0a10932e1c92024dffbb 100644 --- a/stack.c +++ b/stack.c @@ -18,9 +18,13 @@ void stack_peek(stack s, int *value){ } void stack_print(const stack s) { - printf(" TOP\n--------------------\n"); - for (int* spot = s.data + s.top; spot >= s.data; --spot) { - printf("%8d | %12d\n", spot - s.data, *spot); + if (s.top >= 0) { + printf(" TOP\n--------------------\n"); + for (int* spot = s.data + s.top; spot >= s.data; --spot) { + printf("%8d | %12d\n", spot - s.data, *spot); + } + printf("--------------------\n BOTTOM\n"); + } else { + printf("STACK EMPTY\n"); } - printf("--------------------\n BOTTOM\n"); }