Plz check this code i have written.
CODE
/****Implementation of two stacks in a array***/
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
#define MID 5
int a[MAX],ch,count=0;
int top1=-1,top2=MID;
int main()
{
while(1)
{
printf("\t1.PUSH\n");
printf("\t2.POP\n");
printf("\t3.DISPLAY\n");
printf("\t4.EXIT\n");
printf("\tEnter u r choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
}
}
}
push()
{
int n,s;
printf("\tselect a stack..\n\n");
printf("\tType 1 to select a 1.stack\n");
printf("\tType 2 to select a 2.stack\n");
scanf("%d",&s);
switch(s)
{
case 1:
printf("\tEnter elements for stack 1\n");
scanf("%d",&n);
if(top1>=MID)
{
printf("\tStack 1 overflow\n");
return;
}
a[++top1]=n;
break;
case 2:
printf("\tEnter elements for stack 2\n");
scanf("%d",&n);
if(top2 >=MAX)
{
printf("\tStack 2 overflow\n");
return;
}
a[++top2]=n;
}
}
pop()
{
int s;
printf("\t...select a stack to pop up ...\n");
printf("\tType 1 to select a 1.stack\n");
printf("\tType 2 to select a 2.stack\n");
scanf("%d",&s);
switch(s)
{
case 1:
if(top1==-1)
{
printf("\tStack 1 is underflow\n");
return;
}
else
printf("\tdeleted item from the stack 1 is =%d\n",a[top1]);
top1--;
break;
case 2:
if(top2==MID)
{
printf("\tStack 2 is underflow\n");
return;
}
printf("\tDeleted item from the stack 2 is =%d\n",a[top2]);
top2--;
break;
}
}
display()
{
int i;
if(top1==-1)
{
printf("\tStack 1 is empty\n");
}
else
{
printf("\telements in the 1 stack\n");
for(i=top1;i>=0;i--)
printf("\t%d\n",a[i]);
}
if(top2==MID)
printf("\tStack 2 is empty\n");
else
{
printf("\telements in the 2 stack\n");
for(i=top2;i>MID;i--)
printf("\t%d\n",a[i]);
}
}
*1lacca: next time please use code tags, like this: