Skip to content
Related Articles

Related Articles

Implement a stack using single queue
  • Difficulty Level : Easy
  • Last Updated : 12 Feb, 2019

We are given queue data structure, the task is to implement stack using only given queue data structure.

We have discussed a solution that uses two queues. In this article, a new solution is discussed that uses only one queue. This solution assumes that we can find size of queue at any point. The idea is to keep newly inserted element always at rear of queue, keeping order of previous elements same. Below are complete steps.

// x is the element to be pushed and s is stack
push(s, x) 
  1) Let size of q be s. 
  1) Enqueue x to q
  2) One by one Dequeue s items from queue and enqueue them.
  
// Removes an item from stack
pop(s)
  1) Dequeue an item from q

Below is implementation of the idea.

C++




// C++ program to implement a stack using
// single queue
#include<bits/stdc++.h>
using namespace std;
  
// User defined stack that uses a queue
class Stack
{
    queue<int>q;
public:
    void push(int val);
    void pop();
    int top();
    bool empty();
};
  
// Push operation
void Stack::push(int val)
{
    //  Get previous size of queue
    int s = q.size();
  
    // Push current element
    q.push(val);
  
    // Pop (or Dequeue) all previous
    // elements and put them after current
    // element
    for (int i=0; i<s; i++)
    {
        // this will add front element into
        // rear of queue
        q.push(q.front());
  
        // this will delete front element
        q.pop();
    }
}
  
// Removes the top element
void Stack::pop()
{
    if (q.empty())
        cout << "No elements\n";
    else
        q.pop();
}
  
// Returns top of stack
int  Stack::top()
{
    return (q.empty())? -1 : q.front();
}
  
// Returns true if Stack is empty else false
bool Stack::empty()
{
    return (q.empty());
}
  
// Driver code
int main()
{
    Stack s;
    s.push(10);
    s.push(20);
    cout << s.top() << endl;
    s.pop();
    s.push(30);
    s.pop();
    cout << s.top() << endl;
    return 0;
}


Java




// Java program to implement stack using a 
// single queue
  
import java.util.LinkedList;
import java.util.Queue;
  
public class stack 
{
    Queue<Integer> q = new LinkedList<Integer>();
      
    // Push operation
    void push(int val) 
    {
        // get previous size of queue
        int size = q.size();
          
        // Add current element
        q.add(val);
          
        // Pop (or Dequeue) all previous
        // elements and put them after current
        // element
        for (int i = 0; i < size; i++) 
        {
            // this will add front element into
            // rear of queue
            int x = q.remove();
            q.add(x);
        }
    }
      
    // Removes the top element
    int pop() 
    {
        if (q.isEmpty()) 
        {
            System.out.println("No elements");
            return -1;
        }
        int x = q.remove();
        return x;
    }
      
    // Returns top of stack
    int top() 
    {
        if (q.isEmpty())
            return -1;
        return q.peek();
    }
      
    // Returns true if Stack is empty else false
    boolean isEmpty() 
    {
        return q.isEmpty();
    }
  
    // Driver program to test above methods
    public static void main(String[] args) 
    {
        stack s = new stack();
        s.push(10);
        s.push(20);
        System.out.println("Top element :" + s.top());
        s.pop();
        s.push(30);
        s.pop();
        System.out.println("Top element :" + s.top());
    }
}
  
// This code is contributed by Rishabh Mahrsee


C#




// C# program to implement stack using a 
// single queue
using System;
using System.Collections.Generic;
  
public class stack 
{
    Queue<int> q = new Queue<int>();
      
    // Push operation
    void push(int val) 
    {
        // get previous size of queue
        int size = q.Count;
          
        // Add current element
        q.Enqueue(val);
          
        // Pop (or Dequeue) all previous
        // elements and put them after current
        // element
        for (int i = 0; i < size; i++) 
        {
            // this will add front element into
            // rear of queue
            int x = q.Dequeue();
            q.Enqueue(x);
        }
    }
      
    // Removes the top element
    int pop() 
    {
        if (q.Count == 0) 
        {
            Console.WriteLine("No elements");
            return -1;
        }
        int x = q.Dequeue();
        return x;
    }
      
    // Returns top of stack
    int top() 
    {
        if (q.Count == 0)
            return -1;
        return q.Peek();
    }
      
    // Returns true if Stack is empty else false
    bool isEmpty() 
    {
        if(q.Count == 0)
            return true;
        return false;
    }
  
    // Driver program to test above methods
    public static void Main(String[] args) 
    {
        stack s = new stack();
        s.push(10);
        s.push(20);
        Console.WriteLine("Top element :" + s.top());
        s.pop();
        s.push(30);
        s.pop();
        Console.WriteLine("Top element :" + s.top());
    }
}
  
// This code has been contributed by Rajput-Ji 



Output :

20
10

This article is contributed by Manu Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :