Stack Data Structure (Introduction and Program)
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Mainly the following three basic operations are performed in the stack:
- Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
- Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
- Peek or Top: Returns top element of stack.
- isEmpty: Returns true if stack is empty, else false.
program
#include<stdio.h>
#include<conio.h>
void push();
void pop();
void display();
void peep();
int a[10],top=-1;
void main()
{
int ch;
do
{
printf("\n\n MENU");
printf("\n 1.push");
printf("\n 2.pop ");
printf("\n 3.display");
printf("\n 4.peep");
pribtf("\n 5.exit")
printf("Enter Your Choise");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peep();
break;
case 5:
exit(0);
break;
default:
printf("wrong choise");
break;
}while(ch!=5);
void push()
{
int num;
if(top>9)
{
printf("stack is full");
}
else
{
printf("Enter the Number ");
scanf("%d",&num);
top=top+1;
a[top]=num;
}
}
void pop()
{
if(top==-1)
{
printf("stck is empty");
}
else
{
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("stack is empty");
}
else
{
for(i=top;i>=0;i--)
{
printf("%d",&a[i]);
}
}
void peep()
{
if (top==-1)
{
printf("stack is empty");
}
else
{
top=-1;
}
}

Comments
Post a Comment