Learn and interact with data structures in an engaging and visual way.
A stack is a data structure that follows the Last In First Out (LIFO) principle. The last element added to the stack is the first one to be removed.
// Default code (C)
#include
#define MAX 10
int stack[MAX];
int top = -1;
void push(int val) {
if (top >= MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = val;
printf("%d pushed to stack\n", val);
}
}
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
} else {
printf("%d popped from stack\n", stack[top--]);
}
}
void peek() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Top element is %d\n", stack[top]);
}
}
If you have any questions or want to get in touch, feel free to send us a message.
dark - light