Give an algorithm for reversing a queue Q. Only following standard operations are allowed on queue.
- 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 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Output :Q = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10] Input :[1, 2, 3, 4, 5] Output :[5, 4, 3, 2, 1]
C++
// CPP program to reverse a Queue #include <bits/stdc++.h> using namespace std; // Utility function to print the queue void Print(queue<int>& Queue) { while (!Queue.empty()) { cout << Queue.front() << " "; Queue.pop(); } } // Function to reverse the queue void reverseQueue(queue<int>& Queue) { stack<int> Stack; while (!Queue.empty()) { Stack.push(Queue.front()); Queue.pop(); } while (!Stack.empty()) { Queue.push(Stack.top()); Stack.pop(); } } // Driver code int main() { queue<int> Queue; Queue.push(10); Queue.push(20); Queue.push(30); Queue.push(40); Queue.push(50); Queue.push(60); Queue.push(70); Queue.push(80); Queue.push(90); Queue.push(100); reverseQueue(Queue); Print(Queue); } |
chevron_right
filter_none
Java
// Java program to reverse a Queue import java.util.LinkedList; import java.util.Queue; import java.util.Stack; // Java program to reverse a queue public 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(); } } // Function to reverse the queue static void reversequeue() { Stack<Integer> stack = new Stack<>(); while (!queue.isEmpty()) { stack.add(queue.peek()); queue.remove(); } while (!stack.isEmpty()) { queue.add(stack.peek()); stack.pop(); } } // Driver code public static void main(String args[]) { queue = new LinkedList<Integer>(); queue.add(10); queue.add(20); queue.add(30); queue.add(40); queue.add(50); queue.add(60); queue.add(70); queue.add(80); queue.add(90); queue.add(100); reversequeue(); Print(); } } //This code is contributed by Sumit Ghosh |
chevron_right
filter_none
C#
// c# program to reverse a Queue using System; using System.Collections.Generic; public class GFG { public static LinkedList<int> queue; // Utility function to print the queue public static void Print() { while (queue.Count > 0) { Console.Write(queue.First.Value + ", "); queue.RemoveFirst(); } } // Function to reverse the queue public static void reversequeue() { Stack<int> stack = new Stack<int>(); while (queue.Count > 0) { stack.Push(queue.First.Value); queue.RemoveFirst(); } while (stack.Count > 0) { queue.AddLast(stack.Peek()); stack.Pop(); } } // Driver code public static void Main(string[] args) { queue = new LinkedList<int>(); queue.AddLast(10); queue.AddLast(20); queue.AddLast(30); queue.AddLast(40); queue.AddLast(50); queue.AddLast(60); queue.AddLast(70); queue.AddLast(80); queue.AddLast(90); queue.AddLast(100); reversequeue(); Print(); } } // This code is contributed by Shrikant13 |
chevron_right
filter_none
Output:
100, 90, 80, 70, 60, 50, 40, 30, 20, 10
This article is contributed by Raghav Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or 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.
Recommended Posts:
- Reversing the first K elements of a Queue
- Reversing a queue using recursion
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- Queue using Stacks
- Reverse a path in BST using queue
- Priority Queue in Python
- Sharing a queue among three threads
- Queue Interface In Java
- Applications of Priority Queue
- Priority Queue | Set 1 (Introduction)
- Implement a stack using single queue
- Queue | Set 1 (Introduction and Array Implementation)
- Queue | Set 2 (Linked List Implementation)
- Priority Queue using Linked List
Improved By : shrikanth13



