Implementation of Queue using Two Stacks in C++

Implementation of Queue using Two Stacks

 

Queue and stack are fundamentally two different linear data structures, but given with one or more of any one data structures, we can just convert it into other and apply it in real. This article is about the Implementation of Queue using Two Stacks with the help of a Queue named class and two stacks inside it as member variables. 

 
Implementation of Queue using Two Stacks | PrepInsta
Queue with Two Stacks
Queue vs Stack

Stack vs Queue?

Stack is a data structure where you get Last in First out implementation, and queue is with first in first out implementation.

So, it seems impossible to turn a stack into a queue. Though by basic using of two stacks we can implement a queue very easily : Queue By 2 stacks.

Stack Vs Queue

The Algorithm to create a Queue using two Stacks 

Here we are going to discuss the algorithm of how we can make a queue using just two stacks. To visualise it more clearly here a class with the name “Queue” has been created, where there will be just two stacks as member variables and the member functions will be working as the functions to access or manipulate the queue. We will use the stack in C++ STl as the stack.

  • So, we take two stacks, s1 and s2.
    s1 will be working as the main queue and s2 will help us in reversing the order of s1.
  • The basic difference between a stack and a queue is the popping process, that is called Dequeue for the queue. If you think the top of the stack is the back of the queue, the pushing process is the same. But the popping will be in the reverse order. So we will use a stack (s2) temporarily to reverse the values.
  • So, the pushing will be pushing to s1.
  • To pop elements from the back, we will pop out elements from s1 and push it in s2.
  • Then pop only one element.
  • Then again push them into s1. The order of them won’t change for the queue.
  • Same algorithm will be used to check the front value of the queue.
There is another way in which you can turn one stack into a queue. Here it is: Queue using one stack.
Queue using two stacks

The code to implement this:

#include<bits/stdc++.h>
using namespace std;

class Queue
{
  public:
  stack<ints1,s2;
  void Push(int i)
  {
    cout<<“Pushing the element : “<<i<<endl;
    s1.push(i);
  }
  int pop()
  {
    if(s1.empty()) {cout<<“The queue is empty”<<endl;return –1;}
    while(!s1.empty())
    {s2.push(s1.top());s1.pop();}
    int b=s2.top();s2.pop();
    cout<<“Popping the element : “<<b<<endl;
    while(!s2.empty())
    {s1.push(s2.top());s2.pop();}
    return b;
  }
  void Show()
  {
    while(!s1.empty())
    {
        s2.push(s1.top());s1.pop();
    }
    while(!s2.empty())
    {
        cout<<s2.top()<<” “;
        s1.push(s2.top());s2.pop();
    }
  }
  int front()
  {
    if(s1.empty()) {cout<<“The queue is empty”<<endl;return –1;}
    while(!s1.empty())
    {s2.push(s1.top());s1.pop();}
    int b=s2.top();
    while(!s2.empty())
    {s1.push(s2.top());s2.pop();}
    return b;
  }
};

int main()
{
  Queue q;
  q.Push(1);q.Push(5);q.Push(2);q.Push(11);
  cout<<“The Queue now is : “;
  q.Show();cout<<endl;
  cout<<“The front value right now is : “<<q.front()<<endl;
  q.pop();q.pop();
  q.Push(12);q.Push(8);
  cout<<“The front value right now is : “<<q.front()<<endl;
  cout<<“The Queue now is : “;
  q.Show();cout<<endl;
}

Output:

Pushing the element : 1
Pushing the element : 5
Pushing the element : 2
Pushing the element : 11
The Queue now is : 1 5 2 11
The front value right now is : 1
Popping the element : 1
Popping the element : 5
Pushing the element : 12
Pushing the element : 8
The front value right now is : 2
The Queue now is : 2 11 12 8

Article is contributed by Rahit Saha