Beruflich Dokumente
Kultur Dokumente
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define STACK 5
int st[STACK],top=-1;
void push()
{
int item;
if(top==STACK-1)
{
printf("\nStack Overflow..\n");
getch();
return;
}
printf("\nEnter the element to be inserted\n");
scanf("%d",&item);
top++;
st[top]=item;
printf("\n%d Inserted..\n",st[top]);
getch();
}
void pop()
{
int item;
if(top==-1)
{
printf("\nStack Underflow..\n");
getch();
return;
}
printf("\nElement to be popped is %d",st[top]);
item=st[top];
top--;
printf("\nElement popped is %d",item);
getch();
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty..\n");
getch();
return;
}
printf("\nStack elements are:\n");
for(i=0;i<=top;i++)
{
printf("\n%d",st[i]);
}
getch();
}
void main()
{
int ch;
for(;;)
{
clrscr();
printf("\nMenu\n\n1.Push\n2.pop\n3.display\n4.exit\nEnter your choice..\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4: exit(0);
default:printf("\nWrong menu choice\n");
getch();
}
}
}
OUTPUT
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
3
Stack is empty
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
1
11 Inserted
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
1
22 Inserted
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
1
33 Inserted
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
1
44 Inserted
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
1
Enter the element to be inserted
55
55 Inserted
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
1
Stack Overflow
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
3
11
22
33
44
55
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
2
Element to be popped is 55
Element popped is 55
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
2
Element to be popped is 44
Element popped is 44
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
2
Element to be popped is 33
Element popped is 33
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
2
Element to be popped is 22
Element popped is 22
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
2
Element to be popped is 11
Element popped is 11
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
2
Stack Underflow
Menu
1.Push
2.pop
3.display
4.exit
Enter your choice..
4