Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CODING:

MAINTAINING TWO STACKS IN SINGLE ARRAY:


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 10
void push1(int x);
void push2(int x);
void pop1();
void pop2();
void display1();
void display2();
void top1();
void top2();
void poptop1();
void poptop2();
int isempty1();
int isempty2();
void make_empty1();
void make_empty2();
int stack[size],s_top1=-1,s_top2=10,cpos,temp;
void main()
{
int b,x,ch,option,options;
clrscr();
do
{
printf("\n1.Stack1 Operations \n2.Stack2 Operations");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n1.push\n2.pop\n3.Top\n4.Display\n5.Poptop\n6.Make
Empty");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\nEnter the element to be pushed");
scanf("%d",&x);
push1(x);
break;
case 2:pop1();
break;
case 3:top1();
break;
case 4:display1();
break;
case 5:poptop1();
break;
case 6:make_empty1();
break;
}
break;
case 2:printf("\n1.push\n2.pop\n3.Top\n4.Display\n5,Poptop\n6.Make
Empty");
scanf("%d",&options);
switch(options)
{
case 1:
printf("\nEnter the element to be pushed");
scanf("%d",&x);
push2(x);
break;
case 2: pop2();
break;
case 3:top2();
break;
case 4:display2();
break;
case 5:poptop2();
break;
case 6:make_empty2();
break;
}
}
printf("\npress 1 to continue else 0");
scanf("%d",&b);
}while(b==1);
getch();
}
int isempty1()
{
if(s_top1==-1)
return(1);
else
return(0);
}
int isempty2()
{
if(s_top2==10)
return(1);
else
return(0);
}
void push1(int x)
{
if(s_top1==s_top2)
printf("Array overflows");
else
{
s_top1++;
stack[s_top1]=x;
}
}
void push2(int x)
{
if(s_top1==s_top2)
printf("Array overflows");
else
{
s_top2--;
stack[s_top2]=x;
}
}
void pop1()
{
if(isempty1())
printf("Stack is already empty");
else
{
stack[s_top1]=-1;
s_top1--;
}
}
void pop2()
{
if(isempty1())
printf("Stack is already empty");
else
{
stack[s_top2]=10;
s_top2++;
}
}
void display1()
{
int i;
if(isempty1())
printf("Stack is already empty");
else
for(i=s_top1;i!=-1;i--)
{
printf("%d",stack[i]);
printf("\n");
}
}
void display2()
{
int i;
if(isempty2())
printf("Stack is already empty");
else
for(i=s_top2;i!=10;i++)
{
printf("%d",stack[i]);
printf("\n");
}
}
void make_empty1()
{
int i;
if(isempty1())
printf("Already empty");
else
{
for(i=s_top1;i!=-1;i--)
{
stack[i]=-1;
}
s_top1=-1;
}
}
void make_empty2()
{
int i;
if(isempty2())
printf("Already empty");
else
{
for(i=s_top2;i!=10;i++)
{
stack[i]=10;
}
s_top2=10;
}
}
void top1()
{
if(isempty1())
printf("Already empty");
else
{
printf("%d",stack[s_top1]);
printf("\n");
}
}
void top2()
{
if(isempty2())
printf("Already empty");
else
{
printf("%d",stack[s_top2]);
printf("\n");
}
} void poptop1()
{
pop1();
top1();
}
void poptop2()
{
pop2();
top2();
}

You might also like