Reversing a queue using recursion
Last Updated :
17 Mar, 2025
Improve
Try it on GfG Practice
Given a queue, reverse the elements of the queue using recursion. The task is to reverse the order of the elements in the queue and return the reversed queue.
Standard operations allowed:
- enqueue(x): Adds an item x to the rear of the queue.
- dequeue(): Removes an item from the front of the queue.
- empty(): Checks if the 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]
Explanation : Output queue is the reverse of the input queue.
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.
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;
}
}
Implementation:
// C++ code for reversing a queue
#include <bits/stdc++.h>
using namespace std;
// Utility function to print the queue
void printQueue(queue<int> q)
{
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
}
// Recursive function to reverse the queue
void reverseQueue(queue<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 code
int main()
{
queue<int> q;
q.push(56);
q.push(27);
q.push(30);
q.push(45);
q.push(85);
q.push(92);
q.push(58);
q.push(80);
q.push(90);
q.push(100);
reverseQueue(q);
printQueue(q);
}
// Utility function to print the queue
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static void printQueue(Queue<Integer> q) {
while (!q.isEmpty()) {
System.out.print(q.poll() + " ");
}
}
// Recursive function to reverse the queue
static void reverseQueue(Queue<Integer> q) {
// Base case
if (q.isEmpty()) {
return;
}
// Dequeue current item
int data = q.poll();
// Reverse remaining queue
reverseQueue(q);
// Enqueue current item
q.add(data);
}
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(56);
q.add(27);
q.add(30);
q.add(45);
q.add(85);
q.add(92);
q.add(58);
q.add(80);
q.add(90);
q.add(100);
reverseQueue(q);
printQueue(q);
}
}
# Utility function to print the queue
from collections import deque
def print_queue(q):
while q:
print(q.popleft(), end=' ')
# Recursive function to reverse the queue
def reverse_queue(q):
# Base case
if not q:
return
# Dequeue current item (from front)
data = q.popleft()
# Reverse remaining queue
reverse_queue(q)
# Enqueue current item (to rear)
q.append(data)
# Driver code
if __name__ == '__main__':
q = deque()
q.append(56)
q.append(27)
q.append(30)
q.append(45)
q.append(85)
q.append(92)
q.append(58)
q.append(80)
q.append(90)
q.append(100)
reverse_queue(q)
print_queue(q)
// Utility function to print the queue
using System;
using System.Collections.Generic;
class Program {
static void PrintQueue(Queue<int> q) {
while (q.Count > 0) {
Console.Write(q.Dequeue() + " ");
}
}
// Recursive function to reverse the queue
static void ReverseQueue(Queue<int> q) {
// Base case
if (q.Count == 0) {
return;
}
// Dequeue current item
int data = q.Dequeue();
// Reverse remaining queue
ReverseQueue(q);
// Enqueue current item
q.Enqueue(data);
}
static void Main() {
Queue<int> q = new Queue<int>();
q.Enqueue(56);
q.Enqueue(27);
q.Enqueue(30);
q.Enqueue(45);
q.Enqueue(85);
q.Enqueue(92);
q.Enqueue(58);
q.Enqueue(80);
q.Enqueue(90);
q.Enqueue(100);
ReverseQueue(q);
PrintQueue(q);
}
}
// Utility function to print the queue
function printQueue(q) {
while (q.length > 0) {
console.log(q.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.shift();
// Reverse remaining queue
reverseQueue(q);
// Enqueue current item (to rear)
q.push(data);
}
// Driver code
let q = [];
q.push(56);
q.push(27);
q.push(30);
q.push(45);
q.push(85);
q.push(92);
q.push(58);
q.push(80);
q.push(90);
q.push(100);
reverseQueue(q);
printQueue(q);
Output
100 90 80 58 92 85 45 30 27 56
Complexity analysis:
- Time Complexity: O(n).
- Auxiliary Space: O(n), since recursion uses stack internally