You are on page 1of 4

//Two Stacks using Single Array

//
//Vishnu Balachandran//
//4051//
#include<stdio.h>
#include<stdlib.h>
int *a,top1=0,top2,n;
void push1()
{
if(top1+1==top2)
{
printf("overflow\n");
return;
}
top1++;
printf("enter element\n");
scanf("%d",&a[top1]);
return;
}
void push2()
{
if(top1+1==top2)
{
printf("overflow\n");
return;
}
top2--;
printf("enter element\n");
scanf("%d",&a[top2]);
return;
}
void pop1()
{
if(top1==0)
{
printf("underflow\n");
return;
}
printf("the element is %d\n",a[top1]);
top1--;
return;
}
void pop2()
{

if(top2==n)
{
printf("underflow\n");
return;
}
printf("the element is %d\n",a[top2]);
top2++;
return;
}
void main()
{
int opt;
printf("enter n\n");
scanf("%d",&n);
top2=n;
a=malloc(n*sizeof(int));top2=n;
while(1)
{
printf("1.PUSH to 1st\n");
printf("2.POP from 1st\n3.PUSH to 2nd\n");
printf("4.POP from 2nd\n5.exit\n");
scanf("%d",&opt);
if(opt==5)
break;
switch(opt)
{
case 1:
push1();
break;
case 2:
pop1();
break;
case 3:
push2();
break;
case 4:
pop2();
break;
default:
printf("invalid opt");
break;
}
}
}

OUTPUT
enter n
5
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
1
enter element
10
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
1
enter element
20
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
1
enter element
30
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
3
enter element
100
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
3
overflow
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
2
the element is 30

1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
4
the element is 100
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
4
underflow
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
2
the element is 20
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
2
the element is 10
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
2
underflow
1.PUSH to 1st
2.POP from 1st
3.PUSH to 2nd
4.POP from 2nd
5.exit
5