#define maxsize 4
main()
{
int s[maxsize],top=-1;
int choice,data;
void push(int *,int *,int);
int pop(int *,int *,int *);
void display(int *,int);
clrscr();
while(1)
{
printf("\n1:push.....2:pop.....3:display......4:exit\n");
printf("enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nenter the data to be pushed onto the stack\n");
scanf("%d",&data);
push(s,&top,data);
break;
case 2:if(!pop(s,&top,&data))
printf("stack underflow\n");
else
printf("%d popped successfully\n",data);
break;
case 3:display(s,top);
break;
case 4:exit(0);
break;
default:printf("\ninvalid coice");
}
}
}
void push(int *s,int *top,int data)
{
if(*top==maxsize-1)
printf("\nstack overflow");
else
{
*top=*top+1;
s[*top]=data;
printf("\n%dis pushed successfully onto stack",data);
}
}
int pop(int *s,int *top,int *data)
{
if(*top==-1)
return(0);
else
{
*data=s[*top];
*top=*top-1;
return(1);
}
}
void display(int *s,int top)
{ int i;
if(top==-1)
printf("\nstack is empty");
else
{
printf("\nstack contents:-\n");
for(i=0;i<=top;i++)
printf("%d ",s[i]);
}
}
No comments:
Post a Comment