From c34cd59be89fb26e63fe959b98afabb4f24ba5bd Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Wed, 21 Sep 2022 14:16:01 +0300 Subject: init commit --- src/stack.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/stack.c (limited to 'src/stack.c') diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..2fe3488 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,61 @@ +#include "stack.h" +#include + +#define STACK_SIZE 100 + +struct stack_st { + int len; + int arr[STACK_SIZE]; +}; + +struct stack_st stack = { .len = 0, .arr = {0}}; + +// Functions + +void stackPush(int val){ + if (stack.len >= STACK_SIZE){ + printf("Stack overrflow\n"); + return; + } + stack.arr[stack.len] = val; + stack.len++; + return; +} + + +int stackPop(void){ + if (stackIsEmpty()){ + return 0; + } + int stackPopped = stack.arr[--stack.len]; + return stackPopped; +} + + +int stackIsEmpty(void) { + return !(stack.len > 0); +} + + +int stackPeek(){ + // To avoid funking up the stack.len + if (stackIsEmpty()) return 0; + else if (stack.len == 1) return stack.arr[1]; + else{ + int x = stackPop(); + stackPush(x); + return x; + } +} + + +void stackPrint(){ + while(1){ + if(stackIsEmpty()) + break; + + int x = stackPop(); + printf("%d\n", x); + } + return; +} -- cgit v1.2.3