what is stack and program of stack


Ø  Stack –
·         A stack is an Abstract Data Type (ADT), commonly used in most programming languages.
·         It is named stack as it behaves like a real-world stack.
·         Stack is a LIFO (Last in First out) structure or we can say FILO (First in Last out).
·         Push () function is used to insert new elements into the Stack and pop() function is used to remove an element from the stack.
·         Both insertion and removal are allowed at only one end of Stack called Top.
·         Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is completely empty.

program of stack -
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 3 // Altering this value changes size of stack created
int st[MAX],top=-1;
void push();
void pop();
void peek();
void display();
int val;
main()
 {
int option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
}
}while(option != 5);
}
void push()
{
    printf("\n Enter the number to be pushed on stack: ");
    scanf("%d", &val);
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{   
top++;
st[top] = val;
printf("the value inserted in stack is %d",val);
}
}
void pop()
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
}
else
{
val = st[top];
top--;
printf("the value delete is %d",val);
}
}
void display()
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n"); // Added for formatting purposes
}
}
void peek()
{
if(top == -1)
{
printf("\n STACK IS EMPTY");
}
else
printf("the value at top of stack is%d",st[top]);
}