/*Idea is to take 2 queues namely q1 and q2 . Now to perform push() operation in a stack . Insert the element in the empty queue(let's say q1 is empty )and along with that pop() all the elements of the other queue q2 and push() them in queue q1. As queue q2 now become empty so pushing of new element will take place in queue q2 and process repeats. Below is the implementation of the above idea.
Below is the diagrammatic representation of the above idea.
Step 0:
*/
#include<bits/stdc++.h>
using namespace std;
void show_content(queue<int> q)
{
queue<int> temp_queue=q;
cout<<"Contents of the stack are:\n";
if(q.size()==0)
{
cout<<"No elements in stack\n";
}
else
{
while(!temp_queue.empty()){
int d=temp_queue.front();
temp_queue.pop();
cout<<" -\n|";
cout<<d<<"|\n";
}
}
cout<<"\n";
}
void Push(int element,queue<int> &q1,queue<int> &q2)
{
if(q1.empty())
{
q1.push(element);
while(!q2.empty())
{
int value=q2.front();
q2.pop();
q1.push(value);
}
show_content(q1);
}
else
{
if(q2.empty())
{
q2.push(element);
while(!q1.empty())
{
int value=q1.front();
q1.pop();
q2.push(value);
}
show_content(q2);
}
}
}
void Pop(queue<int> &q1,queue<int> &q2)
{
if(!q1.empty()){
q1.pop();
show_content(q1);
}
else{
q2.pop();
show_content(q2);
}
}
int main()
{
int a,b,c;
queue<int> q1;
queue<int> q2;
do
{
cout<<"Press 1 to push element\n";
cout<<"Press 2 to pop element\n";
cout<<"Press 3 to end operations\n";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter the element to be pushed in stack\n";
cin>>b;
Push(b,q1,q2);
break;
case 2:
Pop(q1,q2);
break;
case 3:
cout<<"\n";
return 0;
break;
}
}while(1);
return 0;
}
Below is the diagrammatic representation of the above idea.
Step 0:
"Stack"
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
Queue A Queue B
+---+---+---+---+---+ +---+---+---+---+---+
| | | | | | | | | | | |
+---+---+---+---+---+ +---+---+---+---+---+
Step 1:"Stack"
+---+---+---+---+---+
| 1 | | | | |
+---+---+---+---+---+
Queue A Queue B
+---+---+---+---+---+ +---+---+---+---+---+
| 1 | | | | | | | | | | |
+---+---+---+---+---+ +---+---+---+---+---+
Step 2:"Stack"
+---+---+---+---+---+
| 2 | 1 | | | |
+---+---+---+---+---+
Queue A Queue B
+---+---+---+---+---+ +---+---+---+---+---+
| | | | | | | 2 | 1 | | | |
+---+---+---+---+---+ +---+---+---+---+---+
Step 3:"Stack"
+---+---+---+---+---+
| 3 | 2 | 1 | | |
+---+---+---+---+---+
Queue A Queue B
+---+---+---+---+---+ +---+---+---+---+---+
| 3 | 2 | 1 | | | | | | | | |
+---+---+---+---+---+ +---+---+---+---+---+
Below is the implementation of the above idea.*/
#include<bits/stdc++.h>
using namespace std;
void show_content(queue<int> q)
{
queue<int> temp_queue=q;
cout<<"Contents of the stack are:\n";
if(q.size()==0)
{
cout<<"No elements in stack\n";
}
else
{
while(!temp_queue.empty()){
int d=temp_queue.front();
temp_queue.pop();
cout<<" -\n|";
cout<<d<<"|\n";
}
}
cout<<"\n";
}
void Push(int element,queue<int> &q1,queue<int> &q2)
{
if(q1.empty())
{
q1.push(element);
while(!q2.empty())
{
int value=q2.front();
q2.pop();
q1.push(value);
}
show_content(q1);
}
else
{
if(q2.empty())
{
q2.push(element);
while(!q1.empty())
{
int value=q1.front();
q1.pop();
q2.push(value);
}
show_content(q2);
}
}
}
void Pop(queue<int> &q1,queue<int> &q2)
{
if(!q1.empty()){
q1.pop();
show_content(q1);
}
else{
q2.pop();
show_content(q2);
}
}
int main()
{
int a,b,c;
queue<int> q1;
queue<int> q2;
do
{
cout<<"Press 1 to push element\n";
cout<<"Press 2 to pop element\n";
cout<<"Press 3 to end operations\n";
cin>>a;
switch(a)
{
case 1:
cout<<"Enter the element to be pushed in stack\n";
cin>>b;
Push(b,q1,q2);
break;
case 2:
Pop(q1,q2);
break;
case 3:
cout<<"\n";
return 0;
break;
}
}while(1);
return 0;
}
No comments:
Post a Comment