Saturday 13 December 2014

wap to implement stack

wap to implement stack

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int s[20];
int top;
}st;
int stfull()
{
if(st.top>=19)
return 1;
else
return 0;
}
int stempty()
{
if(st.top==-1)
return 1;
else
return 0;
}
void push(int x)
{
st.top++;
st.s[st.top]=x;
}
int pop()
{
int x;
x=st.s[st.top];
st.top--;
return (x);
}
void display()
{
int i;
if(stempty())
printf("Empty use.");
else
{
for(i=st.top;i>=0;i--)
printf("%d",st.s[i]);
}
}
main()
{
int x,choice;
char ch;
st.top=-1;
do
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("\nEnter your choice=");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter value=");
scanf("%d",&x);
if(stfull())
printf("\nStack is full.");
else
push(x);
break;
case 2:
if(stempty())
printf("\nStack is empty.");
else
x=pop();
printf("\nPopped out element is=%d",x);
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nNo action will be performed.");
}
printf("\nDo you want to continue");
scanf("%c",&ch);
}
while(ch=='y'||ch=='Y');
getch();
}

No comments:

Post a Comment