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.
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]
Approach: For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the queue they would get inserted in reverse order. So now our task is to choose such data-structure which can serve the purpose. According to the approach, the data-structure should have the property of ‘LIFO’ as the last element to be inserted in the data structure should actually be the first element of the reversed queue. The stack could help in approaching this problem. This will be a two-step process:
- Pop the elements from the queue and insert into the stack. (Topmost element of the stack is the last element of the queue)
- Pop the elements of the stack to insert back into the queue. (The last element is the first one to be inserted into the queue)
100, 90, 80, 70, 60, 50, 40, 30, 20, 10
- Time Complexity: O(n).
As we need to insert all the elements in the stack and later to the queue.
- Auxiliary Space: O(N).
Use of stack to store values.
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 firstname.lastname@example.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.
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.
- Reversing a Queue using another Queue
- Reversing a queue using recursion
- Reversing the first K elements of a Queue
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- Difference between Circular Queue and Priority Queue
- Array obtained by repeatedly reversing array after every insertion from given array
- Applications of Queue Data Structure
- Find the largest multiple of 3 | Set 1 (Using Queue)
- Queue Interface In Java
- Implement a stack using single queue
- Circular Queue | Set 1 (Introduction and Array Implementation)
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Queue - Linked List Implementation
- Queue | Set 1 (Introduction and Array Implementation)
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- How to implement stack using priority queue or heap?
- Queue based approach for first non-repeating character in a stream
- Implement Stack and Queue using Deque