From 5a8d23e38d78baf961106975c3f86c5ecb46196a Mon Sep 17 00:00:00 2001 From: "boris.stefanov" <boris.stefanovic@etu.hesge.ch> Date: Mon, 29 Nov 2021 15:42:14 +0100 Subject: [PATCH] * Add stack_print(stack) function (when is_empty is available change implementation slightly) --- stack.c | 17 ++++++++++++++++- stack.h | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/stack.c b/stack.c index 222323a..efeb060 100644 --- a/stack.c +++ b/stack.c @@ -17,6 +17,20 @@ void stack_peek(stack s, int *value){ } } +void stack_print(const stack s) { + //TODO: replace if statement with following as soon as relevant function is implemented + //if (!stack_is_empty()) { + 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"); + } +} + void stack_clone(stack s, stack *clone) { clone->top = s.top; clone->capacity = s.capacity; @@ -24,4 +38,5 @@ void stack_clone(stack s, stack *clone) { for (int i = 0; i <= s.top && i < s.capacity; i++) { clone->data[i] = s.data[i]; } -} \ No newline at end of file +} + diff --git a/stack.h b/stack.h index f1bd309..66056e2 100644 --- a/stack.h +++ b/stack.h @@ -12,4 +12,6 @@ void stack_init(stack *stack); void stack_peek(stack s, int *value); void stack_clone(stack s, stack *clone); +void stack_print(const stack s); + #endif -- GitLab