njvm/stack.c
2023-10-30 00:17:17 +01:00

48 lines
1014 B
C
Executable File

//
// Created by Nilss on 29.10.2023.
//
#ifndef STACK
#define STACK
#include <stdio.h>
#include <stdlib.h>
struct stack {
int* size;
int* current;
unsigned int *stack;
};
void printStack(struct stack stack) {
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);
for (int i = 0; i < *stack.size; ++i) {
printf("|%i|\n", stack.stack[i]);
}
}
struct stack initStack(int size) {
unsigned int a[size];
int current = 0;
struct stack s1 = {&size, &current, a};
return s1;
}
void push(struct stack s, unsigned int value) {
if (*s.current >= *s.size) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
}
s.stack[*s.current] = value;
*s.current=*s.current + 1;
}
unsigned int pop(struct stack s) {
if (*s.current == 0) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
*s.current = *s.current -1;
return s.stack[*s.current];
}
#endif