Explain stacks with the help of an example(Example required).
Stack is a data structure that follows first in last out principle. The first data entered in the stack will be the last one to be removed.
It consist of three main operations:
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isfull() {
if(top == MAXSIZE)
return 1;
else
return 0;
}
int peek() {
return stack[top];
}
int pop() {
int d;
if(!isempty()) {
d = stack[top];
top = top - 1;
return d;
} else {
printf("Stack is empty.\n");
}
}
int push(int d) {
if(!isfull()) {
top = top + 1;
stack[top] = d;
} else {
printf("Stack is full.\n");
}
}
int main() {
push(1);
push(2);
push(3);
return 0;
}
Comments
Leave a comment