Reversing a Stack using two empty Stacks
Last Updated :
24 Aug, 2021
Given a stack S, the task is to reverse the stack S using two additional stacks.
Example:
Input: S={1, 2, 3, 4, 5}
Output: 5 4 3 2 1
Explanation:
The initial stack S:
1→top
2
3
4
5
After reversing it, use two additional stacks:
5→top
4
3
2
1
Input: S={1, 25, 17}
Output: 17 25 1
Approach: Follow the steps below to solve the problem:
- Create two additional empty stacks, say A and B.
- Define a function transfer() that accepts two stacks X and Y as parameters and performs the following operations:
- Loop while X is not empty and perform the following operations:
- Push the top element of the stack X into Y.
- Pop that element from X.
- Call transfer(S, A) tos transfers all elements of the stack S to A. (The order of the elements is reversed).
- Call transfer(A, B) to transfer all elements of the stack A to B. (The order of the elements is the same as initially in S)
- Call transfer(B, S) to transfer all elements of B to S. (The order of the elements is reversed)
- Finally, display the stack S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void transfer(stack< int >& X,
stack< int >& Y)
{
while (!X.empty()) {
Y.push(X.top());
X.pop();
}
}
void display(stack< int > S)
{
while (!S.empty()) {
cout << S.top() << " " ;
S.pop();
}
cout << endl;
}
void reverseStackUsingTwoStacks(stack< int >& S)
{
stack< int > A, B;
transfer(S, A);
transfer(A, B);
transfer(B, S);
display(S);
}
int main()
{
stack< int > S;
S.push(5);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
reverseStackUsingTwoStacks(S);
return 0;
}
|
Java
import java.util.Stack;
public class GFG
{
static void transfer(Stack<Integer> X, Stack<Integer> Y)
{
while (!X.empty())
{
Y.push(X.peek());
X.pop();
}
}
static void display(Stack<Integer> S)
{
while (!S.empty()) {
System.out.print(S.peek() + " " );
S.pop();
}
System.out.println();
}
static void reverseStackUsingTwoStacks(Stack<Integer> S)
{
Stack<Integer> A = new Stack<>();
Stack<Integer> B = new Stack<>();
while (!S.empty())
A.push(S.pop());
while (!A.empty())
B.push(A.pop());
while (!B.empty())
S.push(B.pop());
display(S);
}
public static void main(String[] args)
{
Stack<Integer> S = new Stack<>();
S.push( 5 );
S.push( 4 );
S.push( 3 );
S.push( 2 );
S.push( 1 );
reverseStackUsingTwoStacks(S);
}
}
|
Python3
def display(S):
while len (S) > 0 :
print (S[ - 1 ], end = " " )
S.pop()
print ()
def reverseStackUsingTwoStacks(S):
A = []
B = []
while len (S) > 0 :
A.append(S[ - 1 ])
S.pop()
while len (A) > 0 :
B.append(A[ - 1 ])
A.pop()
while len (B) > 0 :
S.append(B[ - 1 ])
B.pop()
display(S)
S = []
S.append( 5 )
S.append( 4 )
S.append( 3 )
S.append( 2 )
S.append( 1 )
reverseStackUsingTwoStacks(S)
|
C#
using System;
using System.Collections;
class GFG {
static void transfer(Stack X, Stack Y)
{
while (X.Count > 0)
{
Y.Push(X.Peek());
X.Pop();
}
}
static void display(Stack S)
{
while (S.Count > 0) {
Console.Write(S.Peek() + " " );
S.Pop();
}
Console.WriteLine();
}
static void reverseStackUsingTwoStacks(Stack S)
{
Stack A = new Stack();
Stack B = new Stack();
while (S.Count > 0)
A.Push(S.Pop());
while (A.Count > 0)
B.Push(A.Pop());
while (B.Count > 0)
S.Push(B.Pop());
display(S);
}
static void Main() {
Stack S = new Stack();
S.Push(5);
S.Push(4);
S.Push(3);
S.Push(2);
S.Push(1);
reverseStackUsingTwoStacks(S);
}
}
|
Javascript
<script>
function display(S)
{
while (S.length > 0) {
document.write(S[S.length - 1] + " " );
S.pop();
}
document.write( "</br>" );
}
function reverseStackUsingTwoStacks(S)
{
let A = [];
let B = [];
while (S.length > 0)
{
A.push(S[S.length - 1]);
S.pop();
}
while (A.length > 0)
{
B.push(A[A.length - 1]);
A.pop();
}
while (B.length > 0)
{
S.push(B[B.length - 1]);
B.pop();
}
display(S);
}
let S = [];
S.push(5);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
reverseStackUsingTwoStacks(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...