Related Articles

# Reversing a queue using recursion

• Difficulty Level : Easy
• Last Updated : 24 Jun, 2021

Given a queue, write a recursive function to reverse it.
Standard operations allowed :
enqueue(x) : Add an item x to rear of queue.
dequeue() : Remove an item from front of queue.
empty() : Checks if a queue is empty or not.
Examples :

Input : Q = [5, 24, 9, 6, 8, 4, 1, 8, 3, 6]
Output : Q = [6, 3, 8, 1, 4, 8, 6, 9, 24, 5]

Explanation : Output queue is the reverse of the input queue.

Input : Q = [8, 7, 2, 5, 1]
Output : Q = [1, 5, 2, 7, 8]

Recursive Algorithm :

1. The pop element from the queue if the queue has elements otherwise return empty queue.
2. Call reverseQueue function for the remaining queue.
3. Push the popped element in the resultant reversed queue.

Pseudo Code :

queue reverseFunction(queue)
{
if (queue is empty)
return queue;
else {
data = queue.front()
queue.pop()
queue = reverseFunction(queue);
q.push(data);
return queue;
}
}

## C++

 // C++ code for reversing a queue#include using namespace std; // Utility function to print the queuevoid printQueue(queue Queue){    while (!Queue.empty()) {        cout << Queue.front() << " ";        Queue.pop();    }} // Recursive function to reverse the queuevoid reverseQueue(queue& q){    // Base case    if (q.empty())        return;     // Dequeue current item (from front)     long long int data = q.front();    q.pop();     // Reverse remaining queue     reverseQueue(q);     // Enqueue current item (to rear)     q.push(data);} // Driver codeint main(){    queue Queue;    Queue.push(56);    Queue.push(27);    Queue.push(30);    Queue.push(45);    Queue.push(85);    Queue.push(92);    Queue.push(58);    Queue.push(80);    Queue.push(90);    Queue.push(100);    reverseQueue(Queue);    printQueue(Queue);}

## Python3

 from queue import Queue  def reverse_queue(queue: Queue):    # Base case    if queue.empty():        return     # Dequeue current item (from front)    item = queue.queue[0]    queue.get()     # Reverse remaining queue    reverse_queue(queue)     # Enqueue current item (to rear)    queue.put(item)  def print_queue(queue: Queue):    while not queue.empty():        print(queue.queue[0], end=" ")        queue.get()    print()  # Driver Codeif __name__ == "__main__":    q = Queue()    q.put(56)    q.put(27)    q.put(30)    q.put(45)    q.put(85)    q.put(92)    q.put(58)    q.put(80)    q.put(90)    q.put(100)     reverse_queue(q)    print_queue(q)

## C#

 // C# code for reversing a queueusing System;using System.Collections.Generic; class GFG{    // Utility function    // to print the queue    static void printQueue(Queue queue)    {        while (queue.Count != 0)        {            Console.Write(queue.Peek() + " ");            queue.Dequeue();        }    }         // Recursive function    // to reverse the queue    static void reverseQueue(ref Queue q)    {        // Base case        if (q.Count == 0)            return;             // Dequeue current        // item (from front)        long data = q.Peek();        q.Dequeue();             // Reverse remaining queue        reverseQueue(ref q);             // Enqueue current        // item (to rear)        q.Enqueue(data);    }             // Driver code    static void Main()    {        Queue queue = new Queue();        queue.Enqueue(56);        queue.Enqueue(27);        queue.Enqueue(30);        queue.Enqueue(45);        queue.Enqueue(85);        queue.Enqueue(92);        queue.Enqueue(58);        queue.Enqueue(80);        queue.Enqueue(90);        queue.Enqueue(100);        reverseQueue(ref queue);        printQueue(queue);    }} // This code is contributed by// Manish Shaw(manishshaw1)
Output:
100 90 80 58 92 85 45 30 27 56

Time Complexity : O(n).
Video:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up