#include<stdio.h>
void push(int *,int *,int []);
int pop(int *,int []);
void display(int *,int []);
int size;
main()
{
int top=-1;
int item,s[50];
int choice;
int flag;
printf("\n..ENTER THE SIZE OF THE STACK..\n");
scanf("%d",&size);
do
{
printf("\nMENU\n1.PUSH\n2.POP\n3.EXIT\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nENTER THE ELEMENT YOU WANT TO PUSH\n");
scanf("%d",&item);
push(&item,&top,s);
display(&top,s);
break;
case 2:
flag=pop(&top,s);
if(flag==-1)
{
printf("\n...THE STACK IS EMPTY...\n");
}
else
printf("\n...THE POPPED OUT ELEMENT..%d",flag);
printf("\n");
display(&top,s);
break;
case 3:
printf("\n...YOU HAVE EXITED..\n");
break;
}
}while(choice!=3);
}
void push(int *item,int *top,int s[50])
{
if(*top==size-1)
{
printf("\n THE STACK IS FULL \n");
return;
}
s[++(*top)]=*item;
}
int pop(int *top,int s[50])
{
if(*top==-1)
{
return -1;
}
return s[(*top)--];
}
void display(int *top,int s[50])
{
int i;
for(i=*top;i>=0;i--)
{
if(i==*top)
{
printf("TOP->%d\n",s[i]);
}
else
printf(" %d\n",s[i]);
}
}
void push(int *,int *,int []);
int pop(int *,int []);
void display(int *,int []);
int size;
main()
{
int top=-1;
int item,s[50];
int choice;
int flag;
printf("\n..ENTER THE SIZE OF THE STACK..\n");
scanf("%d",&size);
do
{
printf("\nMENU\n1.PUSH\n2.POP\n3.EXIT\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nENTER THE ELEMENT YOU WANT TO PUSH\n");
scanf("%d",&item);
push(&item,&top,s);
display(&top,s);
break;
case 2:
flag=pop(&top,s);
if(flag==-1)
{
printf("\n...THE STACK IS EMPTY...\n");
}
else
printf("\n...THE POPPED OUT ELEMENT..%d",flag);
printf("\n");
display(&top,s);
break;
case 3:
printf("\n...YOU HAVE EXITED..\n");
break;
}
}while(choice!=3);
}
void push(int *item,int *top,int s[50])
{
if(*top==size-1)
{
printf("\n THE STACK IS FULL \n");
return;
}
s[++(*top)]=*item;
}
int pop(int *top,int s[50])
{
if(*top==-1)
{
return -1;
}
return s[(*top)--];
}
void display(int *top,int s[50])
{
int i;
for(i=*top;i>=0;i--)
{
if(i==*top)
{
printf("TOP->%d\n",s[i]);
}
else
printf(" %d\n",s[i]);
}
}
No comments:
Post a Comment