//this code is designed by codenation.
//12:37 6/12/2020
#include<stdio.h>
int push(int stack[],int maxstack,int item);
int pop(int stack[]);
int maxstack;
int stack[1];
int top=0;
int flag=0;
int main()
{
int item,ch,i;
printf("enter the size of stack : \n");
scanf("%d",&maxstack);
int stack[maxstack];
maxstack=maxstack-1;
while(1)
{
printf("which operation you want to perform on stack:\n");
printf("enter your choice :\n");
printf("1.PUSH\n2.POP\n3.VIEW STACK\n4.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the element you want to push to stack :\n");
scanf("%d",&item);
push(stack,maxstack,item);
break;
case 2:
printf("\nyou are going to pop element at top....\n");
pop(stack);
break;
case 3:
if(flag==0)
{
printf("\nthe stack is empty.\n\n");
}
else
{
printf("\nthe stack is :\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
printf("\n");
}
break;
case 4:
return 0;
default:
printf("\nenter the correct choice.\n\n");
}
}
}
int push(int stack[],int maxstack,int item)
{
if(top==maxstack && flag!=0)
{
printf("\nstack overflow!!!\n\n");
return 0;
}
else
{
if(flag==0)
{
stack[top]=item;
flag=1;
}
else
{
top=top+1;
stack[top]=item;
}
}
printf("\nelement pushed successfully.\n\n");
return 0;
}
int pop(int stack[])
{
if(flag==0)
{
printf("\nstack underflow!!!\n\n");
return 0;
}
else if(top==0)
{
flag=0;
}
else
{
top=top-1;
}
printf("\nelement popped successfully.\n\n");
return 0;
}
//this code is developed by codenation.
Comments
Post a Comment