summaryrefslogtreecommitdiff
path: root/src/stack.c
diff options
context:
space:
mode:
authorRasmus Luha <rasmus.luha@gmail.com>2022-09-21 14:16:01 +0300
committerRasmus Luha <rasmus.luha@gmail.com>2022-09-21 14:16:01 +0300
commitc34cd59be89fb26e63fe959b98afabb4f24ba5bd (patch)
tree60531983cb0bfef27ab91cb3be41ebd40b324c9f /src/stack.c
init commit
Diffstat (limited to 'src/stack.c')
-rw-r--r--src/stack.c61
1 files changed, 61 insertions, 0 deletions
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 <stdio.h>
+
+#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;
+}