Reversing a queue using recursion

13 Jun, 2022
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 :  
The pop element from the queue if the queue has elements otherwise return empty queue.Call reverseQueue function for the remaining queue.Push the popped element in the resultant reversed queue.
The pop element from the queue if the queue has elements otherwise return empty queue.
Call reverseQueue function for the remaining queue.
Push the popped element in the resultant reversed queue.
 
Pseudo Code :  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
queue reverseFunction(queue)
{
    if (queue is empty)
       return queue;
    else {
       data = queue.front()
       queue.pop()
       queue = reverseFunction(queue);
       q.push(data);
       return queue;
    }
}
 
 
C++
Java
Python3
C#
Javascript
// C++ code for reversing a queue#include <bits/stdc++.h>using namespace std; // Utility function to print the queuevoid printQueue(queue<long long int> Queue){    while (!Queue.empty()) {        cout << Queue.front() << " ";        Queue.pop();    }} // Recursive function to reverse the queuevoid reverseQueue(queue<long long int>& 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<long long int> 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);}
// Java program to reverse a Queue by recursionimport java.util.LinkedList;import java.util.Queue;import java.util.Stack;  // Java program to reverse a queue recursivelypublic class Queue_reverse {      static Queue<Integer> queue;      // Utility function to print the queue    static void Print()    {        while (!queue.isEmpty())        {            System.out.print(queue.peek() + " ");            queue.remove();        }    }  // Recurrsive function to reverse the queuestatic Queue<Integer> reverseQueue(Queue<Integer> q){    // Base case    if (q.isEmpty())        return q;     // Dequeue current item (from front)     int data = q.peek();    q.remove();     // Reverse remaining queue     q = reverseQueue(q);     // Enqueue current item (to rear)     q.add(data);             return q;}  // Driver codepublic static void main(String args[]){    queue = new LinkedList<Integer>();    queue.add(56);    queue.add(27);    queue.add(30);    queue.add(45);    queue.add(85);    queue.add(92);    queue.add(58);    queue.add(80);    queue.add(90);    queue.add(100);    queue = reverseQueue(queue);    Print();}}
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# code for reversing a queueusing System;using System.Collections.Generic; class GFG{    // Utility function    // to print the queue    static void printQueue(Queue<long> queue)    {        while (queue.Count != 0)        {            Console.Write(queue.Peek() + " ");            queue.Dequeue();        }    }         // Recursive function    // to reverse the queue    static void reverseQueue(ref Queue<long> 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<long> queue = new Queue<long>();        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)
<script>    // Javascript code for reversing a queue         // Utility function    // to print the queue    function printQueue(queue)    {        while (queue.length != 0)        {            document.write(queue[0] + " ");            queue.shift();        }    }          // Recursive function    // to reverse the queue    function reverseQueue(q)    {        // Base case        if (q.length == 0)            return;              // Dequeue current        // item (from front)        let data = q[0];        q.shift();              // Reverse remaining queue        reverseQueue(q);              // Enqueue current        // item (to rear)        q.push(data);    }            let 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); // This code is contributed by divyeshrabadiya07.</script>
100 90 80 58 92 85 45 30 27 56
 
Time Complexity: O(n). 
Auxiliary Space: O(n), since recursion uses stack internallyVideo:  
Reversing a queue using recursion | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersReversing a queue using recursion | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:21•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=4tirwGyMtUw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
manishshaw1
ParulShandilya
shindesharad71
divyeshrabadiya07
technophpfij
cpp-queue
java-queue
Queue
Recursion
Technical Scripter
Recursion
Queue
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.