Reversing a Queue using another Queue Last Updated : 11 Jul, 2022 Comments Improve Suggest changes Like Article Like Report Given a queue. The task is to reverse the queue using another empty queue. Examples: Input: queue[] = {1, 2, 3, 4, 5} Output: 5 4 3 2 1 Input: queue[] = {10, 20, 30, 40} Output: 40 30 20 10 Approach: Given a queue and an empty queue.The last element of the queue should be the first element of the new queue.To get the last element there is a need to pop the queue one by one and add it to the end of the queue, size - 1 times.So after that, we will get the last element in front of the queue. Now pop that element out and add it to the new queue. Repeat the steps s - 1 times where s is the original size of the queue. Below is the implementation of the approach: C++ // C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to return the reversed queue queue<int> reverse(queue<int> q) { // Size of queue int s = q.size(); // Second queue queue<int> ans; for (int i = 0; i < s; i++) { // Get the last element to the // front of queue for (int j = 0; j < q.size() - 1; j++) { int x = q.front(); q.pop(); q.push(x); } // Get the last element and // add it to the new queue ans.push(q.front()); q.pop(); } return ans; } // Driver Code int main() { queue<int> q; // Insert elements q.push(1); q.push(2); q.push(3); q.push(4); q.push(5); q = reverse(q); // Print the queue while (!q.empty()) { cout << q.front() << " "; q.pop(); } return 0; } Java // Java implementation of the above approach import java.util.*; class GFG { // Function to return the reversed queue static Queue<Integer> reverse(Queue<Integer> q) { // Size of queue int s = q.size(); // Second queue Queue<Integer> ans = new LinkedList<>(); for (int i = 0; i < s; i++) { // Get the last element to the // front of queue for (int j = 0; j < q.size() - 1; j++) { int x = q.peek(); q.remove(); q.add(x); } // Get the last element and // add it to the new queue ans.add(q.peek()); q.remove(); } return ans; } // Driver Code public static void main(String[] args) { Queue<Integer> q = new LinkedList<>(); // Insert elements q.add(1); q.add(2); q.add(3); q.add(4); q.add(5); q = reverse(q); // Print the queue while (!q.isEmpty()) { System.out.print(q.peek() + " "); q.remove(); } } } // This code is contributed by Princi Singh Python3 # Python3 implementation of the above approach from collections import deque # Function to return the reversed queue def reverse(q): # Size of queue s = len(q) # Second queue ans = deque() for i in range(s): # Get the last element to the # front of queue for j in range(s - 1): x = q.popleft() q.appendleft(x) # Get the last element and # add it to the new queue ans.appendleft(q.popleft()) return ans # Driver Code q = deque() # Insert elements q.append(1) q.append(2) q.append(3) q.append(4) q.append(5) q = reverse(q) # Print the queue while (len(q) > 0): print(q.popleft(), end = " ") # This code is contributed by Mohit Kumar C# // C# Program to print the given pattern using System; using System.Collections.Generic; class GFG { // Function to return the reversed queue static Queue<int> reverse(Queue<int> q) { // Size of queue int s = q.Count; // Second queue Queue<int> ans = new Queue<int>(); for (int i = 0; i < s; i++) { // Get the last element to the // front of queue for (int j = 0; j < q.Count - 1; j++) { int x = q.Peek(); q.Dequeue(); q.Enqueue(x); } // Get the last element and // add it to the new queue ans.Enqueue(q.Peek()); q.Dequeue(); } return ans; } // Driver Code public static void Main(String[] args) { Queue<int> q = new Queue<int>(); // Insert elements q.Enqueue(1); q.Enqueue(2); q.Enqueue(3); q.Enqueue(4); q.Enqueue(5); q = reverse(q); // Print the queue while (q.Count!=0) { Console.Write(q.Peek() + " "); q.Dequeue(); } } } // This code is contributed by Princi Singh JavaScript <script> // Javascript implementation of the above approach // Function to return the reversed queue function reverse(q) { // Size of queue let s = q.length; // Second queue let ans = []; for(let i = 0; i < s; i++) { // Get the last element to the // front of queue for(let j = 0; j < q.length - 1; j++) { let x = q.shift(); q.push(x); } // Get the last element and // add it to the new queue ans.push(q[0]); q.shift(); } return ans; } // Driver Code let q = []; // Insert elements q.push(1); q.push(2); q.push(3); q.push(4); q.push(5); q = reverse(q); // Print the queue while (q.length != 0) { document.write(q[0] + " "); q.shift(); } // This code is contributed by patel2127 </script> Output: 5 4 3 2 1 Time Complexity: O(n2) Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Reversing a Queue using another Queue A andrew1234 Follow Improve Article Tags : Queue Competitive Programming DSA Data Structures Java-Queue-Programs +1 More Practice Tags : Data StructuresQueue Similar Reads Reversing a queue using recursion 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() 4 min read Stack and Queue in Python using queue Module A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generall 3 min read Reversing a Queue You are given a queue Q, and your task is to reverse the elements of the queue. You are only allowed to use the following standard queue operations:enqueue(x): Add an item x to the rear of the queue.dequeue(): Remove an item from the front of the queue.empty(): Check if the queue is empty or not.Exa 4 min read Reverse a Stack using Queue Given a stack, the task is to reverse the stack using the queue data structure. Examples: Input: Stack: (Top to Bottom) [10 -> 20 -> 30 -> 40]Output: Stack: (Top to Bottom) [40 -> 30 -> 20 -> 10] Input: Stack: [6 -> 5 -> 4]Output: Stack: [4 -> 5 -> 6] Approach: The prob 5 min read Check if a queue can be sorted into another queue using a stack Given a Queue consisting of first n natural numbers (in random order). The task is to check whether the given Queue elements can be arranged in increasing order in another Queue using a stack. The operation allowed are: Push and pop elements from the stack Pop (Or Dequeue) from the given Queue. Push 9 min read Like