Reverse a Stack using Queue
Last Updated :
22 Dec, 2022
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 problem can be solved based on the following idea:
The stack follows LIFO order and the queue follows FIFO order. So, if we push all stack elements into queue and then push it back into the stack it will be reversed.
Illustration:
Consider Stack: [40 -> 30 -> 20 -> 10]
Step 1:
- Stack: (Bottom to Top) [40 -> 30 -> 20 -> 10]
Queue: (Front to Rear) Empty
Step 2:
- Stack: (Bottom to Top) [40 -> 30 -> 20]
Queue: (Front to Rear) 10
Step 3:
- Stack: (Bottom to Top) [40 -> 30]
Queue: (Front to Rear) [10 -> 20]
Step 4:
- Stack: (Bottom to Top) [40]
Queue: (Front to Rear) [10 -> 20 -> 30]
Step 5:
- Stack: (Bottom to Top) Empty
Queue: (Front to Rear) [10 -> 20 -> 30 -> 40]
Step 6:
- Stack: (Bottom to Top) [10]
Queue: (Front to Rear) [20 -> 30 -> 40]
Step 7:
- Stack: (Bottom to Top) [10 -> 20]
Queue: (Front to Rear) [30 -> 40]
Step 8:
- Stack: (Bottom to Top) [10 -> 20 -> 30]
Queue: (Front to Rear) [40]
Step 9:
- Stack: (Bottom to Top) [10 -> 20 -> 30 -> 40]
Queue: (Front to Rear) Empty
Follow the steps to solve the problem:
- Pop Elements one by one from the stack.
- Enqueue every popped element into the queue.
- Do it till the stack is empty.
- Then, Dequeue elements one by one from the queue.
- Push every dequeued element into the stack.
- Do it till the queue is empty.
- The stack is now reversed.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void reverse(stack< int >& stk)
{
queue< int > qu;
while (!stk.empty()) {
qu.push(stk.top());
stk.pop();
}
while (!qu.empty()) {
stk.push(qu.front());
qu.pop();
}
}
int main()
{
stack< int > stk;
stk.push(40);
stk.push(30);
stk.push(20);
stk.push(10);
reverse(stk);
cout << "After Reverse : (Top to Bottom)"
<< "\n" ;
while (!stk.empty()) {
cout << stk.top() << " " ;
stk.pop();
}
cout << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void reverse(Stack<Integer> stk)
{
Queue<Integer> qu = new LinkedList<>();
while (!stk.isEmpty()) {
qu.add(stk.peek());
stk.pop();
}
while (!qu.isEmpty()) {
stk.push(qu.peek());
qu.poll();
}
}
public static void main(String[] args)
{
Stack<Integer> stk = new Stack<>();
stk.push( 40 );
stk.push( 30 );
stk.push( 20 );
stk.push( 10 );
reverse(stk);
System.out.println(
"After Reverse : (Top to Bottom)" );
while (!stk.isEmpty()) {
System.out.print(stk.peek() + " " );
stk.pop();
}
System.out.println();
}
}
|
Python3
from collections import deque
def reverse(stack):
queue = deque()
while stack ! = []:
queue.append(stack.pop())
while queue:
stack.append(queue.popleft())
return stack
stack = []
stack.append( 40 )
stack.append( 30 )
stack.append( 20 )
stack.append( 10 )
stack = reverse(stack)
print ( "After Reverse : (Top to Bottom)" )
while stack ! = []:
print (stack[ - 1 ], end = " " )
stack.pop()
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void Reverse(Stack< int > stk)
{
Queue< int > qu = new Queue< int >();
while (stk.Count > 0) {
qu.Enqueue(stk.Peek());
stk.Pop();
}
while (qu.Count > 0) {
stk.Push(qu.Peek());
qu.Dequeue();
}
}
static public void Main()
{
Stack< int > stk = new Stack< int >();
stk.Push(40);
stk.Push(30);
stk.Push(20);
stk.Push(10);
Reverse(stk);
Console.WriteLine(
"After Reverse : (Top to Bottom)" );
while (stk.Count > 0) {
Console.Write(stk.Peek() + " " );
stk.Pop();
}
Console.WriteLine();
}
}
|
Javascript
function reverse(stk) {
let qu = [];
while (stk.length > 0) {
qu.push(stk.pop());
}
while (qu.length > 0) {
stk.push(qu.shift());
}
}
let stk = [10, 20, 30, 40];
reverse(stk);
console.log( "After Reverse : (Top to Bottom)" );
while (stk.length > 0) {
console.log(stk.pop() + " " );
}
|
Output
After Reverse : (Top to Bottom)
40 30 20 10
Time Complexity: O(N), As we are popping out N elements, then only pushing N elements. N+N operations.
Auxiliary Space: O(N), As we are using an Extra Queue of N Space.
Related articles:
Share your thoughts in the comments
Please Login to comment...