### Implentation of Queue using Two Stacks

#include<iostream>
using namespace std;

/*DESCRIPTION
this is c++ program to implement queue with the help
of two Stacks.
LOGIC for different queue operation-
1.Insert- Elemnts are pushed in stack1
2.Remove- Elemets of stack1 is poped and pushed
in stack 2 one by one and then remove top element then
elemets of stack2 is poped and pushed in stack 1 one by one
3.Display-Elemets of stack 1 are displayed from val 0 to top */

int stack1[100],stack2[100],n=100,top1=-1,top2=-1;
void push(int x,int y[],int* z)
{
if(*z==n-1)
cout<<"Queue Overflow\n";   //for PUSH operation
else
{

y[++*z]=x;
}

}

int pop(int y[],int* z)
{
if(*z==-1)
cout<<"Queue Underflow\n";   //for Pop Operation
else
{

return y[*z];
}

}

void display()
{

cout<<"Queue\n";
for(int i=0;i<=top1;i++)      //for Display Operation
{
cout<<stack1[i]<<endl;
}
}
int main()
{
int ch,t,a,b;
cout<<"1.Insert in Queue\n";
cout<<"2.Remove from Queue\n";
cout<<"3.Display the Queue\n";
cout<<"4.Exit\n";
do
{
cin>>ch;
switch(ch)
{
case 1:
int val;
cout<<"Enter the value\n";
cin>>val;
push(val,stack1,&top1);
break;
case 2:
a=top1;
for(int i=0;i<=a;i++)
{
t=pop(stack1,&top1);
top1--;
push(t,stack2,&top2);
}
cout<<"Removed Element is "<<stack2[top2]<<endl;
pop(stack2,&top2);
top2--;

b=top2;
for(int i=0;i<=b;i++)
{
t=pop(stack2,&top2);
top2--;
push(t,stack1,&top1);
}

break;
case 3:
display();
break;
case 4:
cout<<"Exit\n";
break;
default:
cout<<"Wrong Choice\n";

}
}while(ch!=4);
}
}OUTPUT

#### More Articles of Rajkumar unknown:

Name Views Likes
Implentation of Queue using Two Stacks 250 3