Open In App

Implement Stack using Queues

Last Updated : 25 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Implement a stack using queues. The stack should support the following operations:

  1. Push(x): Push an element onto the stack.
  2. Pop(): Pop the element from the top of the stack and return it.

Stack and Queue with insert and delete operations

A Stack can be implemented using two queues. Let Stack to be implemented be ‘s’ and queues used to implement are ‘q1’ and ‘q2’.
Stack ‘s’ can be implemented in two ways:

By making push() operation costly – Push in O(n) and Pop() in O(1)

The idea is to keep newly entered element at the front of ‘q1’ so that pop operation dequeues from ‘q1’. ‘q2’ is used to move every new element in front of ‘q1’.

Follow the below steps to implement the push(s, x) operation: 

  • Enqueue x to q2.
  • One by one dequeue everything from q1 and enqueue to q2.
  • Swap the queues of q1 and q2.

Follow the below steps to implement the pop(s) operation: 

  • Dequeue an item from q1 and return it.
C++
/* Program to implement a stack using
two queue */
#include <bits/stdc++.h>

using namespace std;

class Stack {
    // Two inbuilt queues
    queue<int> q1, q2;

public:
    void push(int x)
    {
        // Push x first in empty q2
        q2.push(x);

        // Push all the remaining
        // elements in q1 to q2.
        while (!q1.empty()) {
            q2.push(q1.front());
            q1.pop();
        }

        // swap the names of two queues
        swap(q1, q2);
    }

    void pop()
    {
        // if no elements are there in q1
        if (q1.empty())
            return;
        q1.pop();
    }

    int top()
    {
        if (q1.empty())
            return -1;
        return q1.front();
    }

    int size() { return q1.size(); }
};

// Driver code
int main()
{
    Stack s;
    s.push(1);
    s.push(2);
    s.push(3);

    cout << "current size: " << s.size() << endl;
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;

    cout << "current size: " << s.size() << endl;
    return 0;
}
Java Python C# JavaScript

Output
current size: 3
3
2
1
current size: 1

Time Complexity:

  • Push operation: O(n), As all the elements need to be popped out from the Queue (q1) and push them back to Queue (q2).
  • Pop operation: O(1), As we need to remove the front element from the Queue.

Auxiliary Space: O(n), As we use two queues for the implementation of a Stack.

By making pop() operation costly – Push in O(1) and Pop() in O(n)

The new element is always enqueued to q1. In pop() operation, if q2 is empty then all the elements except the last, are moved to q2. Finally, the last element is dequeued from q1 and returned.

Follow the below steps to implement the push(s, x) operation: 

  • Enqueue x to q1 (assuming the size of q1 is unlimited).

Follow the below steps to implement the pop(s) operation: 

  • One by one dequeue everything except the last element from q1 and enqueue to q2.
  • Dequeue the last item of q1, the dequeued item is the result, store it.
  • Swap the names of q1 and q2
  • Return the item stored in step 2.
C++
// Program to implement a stack
// using two queue
#include <bits/stdc++.h>
using namespace std;

class Stack {
    queue<int> q1, q2;

public:
    void pop()
    {
        if (q1.empty())
            return;

        // Leave one element in q1 and
        // push others in q2.
        while (q1.size() != 1) {
            q2.push(q1.front());
            q1.pop();
        }

        // Pop the only left element
        // from q1
        q1.pop();

        // swap the names of two queues
        swap(q1, q2);
    }

    void push(int x) { q1.push(x); }

    int top()
    {
        if (q1.empty())
            return -1;

        while (q1.size() != 1) {
            q2.push(q1.front());
            q1.pop();
        }

        // last pushed element
        int temp = q1.front();

        // to empty the auxiliary queue after
        // last operation
        q1.pop();

        // push last element to q2
        q2.push(temp);

        // swap the two queues names
        queue<int> q = q1;
        q1 = q2;
        q2 = q;
        return temp;
    }

    int size() { return q1.size(); }
};

// Driver code
int main()
{
    Stack s;
    s.push(1);
    s.push(2);
    s.push(3);

    cout << "current size: " << s.size() << endl;
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
    s.pop();
    cout << s.top() << endl;
    cout << "current size: " << s.size() << endl;
    return 0;
}
Java Python C# JavaScript

Output
current size: 3
3
2
1
current size: 1

Time Complexity: 

  • Push operation: O(1), As, on each push operation the new element is added at the end of the Queue.
  • Pop operation: O(n), As, on each pop operation, all the elements are popped out from the Queue (q1) except the last element and pushed into the Queue (q2).

Auxiliary Space: O(n) since 2 queues are used.

Using single queue and Recursion Stack

Using only one queue and make the queue act as a Stack in modified way of the above discussed approach.

Follow the below steps to implement the idea: 

  • The idea behind this approach is to make one queue and push the first element in it. 
  • After the first element, we push the next element and then push the first element again and finally pop the first element. 
  • So, according to the FIFO rule of the queue, the second element that was inserted will be at the front and then the first element as it was pushed again later and its first copy was popped out. 
  • So, this acts as a Stack and we do this at every step i.e. from the initial element to the second last element, and the last element will be the one that we are inserting and since we will be pushing the initial elements after pushing the last element, our last element becomes the first element.
C++
#include <bits/stdc++.h>
using namespace std;

// Stack Class that acts as a queue
class Stack {

    queue<int> q;

public:
    void push(int data)
    {
        int s = q.size();

        // Push the current element
        q.push(data);

        // Pop all the previous elements and put them after
        // current element

        for (int i = 0; i < s; i++) {
            
            // Add the front element again
            q.push(q.front());

            // Delete front element
            q.pop();
        }
    }
    void pop()
    {
        if (q.empty())
            cout << "No elements\n";
        else
            q.pop();
    }
    int top() { return (q.empty()) ? -1 : q.front(); }
    int size() { return q.size(); }
    bool empty() { return (q.empty()); }
};

int main()
{
    Stack st;
    st.push(1);
    st.push(2);
    st.push(3);
    cout << "current size: " << st.size() << "\n";
    cout << st.top() << "\n";
    st.pop();
    cout << st.top() << "\n";
    st.pop();
    cout << st.top() << "\n";
    cout << "current size: " << st.size();
    return 0;
}
Java Python C# JavaScript

Output
current size: 3
3
2
1
current size: 1

Time Complexity:

  • Push operation: O(n)
  • Pop operation: O(1)

Auxiliary Space: O(n) since 1 queue is used.




Next Article

Similar Reads

three90RightbarBannerImg