Clone a stack without using extra space | Set 2
Last Updated :
25 Aug, 2021
Given a stack S, the task is to copy the content of the given stack S to another stack T maintaining the same order.
Examples:
Input: Source:- |5|
|4|
|3|
|2|
|1|
Output: Destination:- |5|
|4|
|3|
|2|
|1|
Input: Source:- |12|
|13|
|14|
|15|
|16|
Output: Destination:- |12|
|13|
|14|
|15|
|16|
Reversing the Stack-Based Approach: Please refer to the previous post of this article for reversing the stack-based approach.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Linked List-Based Approach: Please refer to the previous post of this article for the linked list-based approach.
Time Complexity: O(N)
Auxiliary Space: O(1)
Recursion-Based Approach: The given problem can also be solved by using recursion. Follow the steps below to solve the problem:
- Define a recursive function, say RecursiveCloneStack(stack<int> S, stack<int>Des) where S is the source stack and Des is the destination stack:
- Initialize a stack, say Des to store the destination stack.
- Now call the function RecursiveCloneStack(S, Des) to copy the elements of the source stack to the destination stack.
- After completing the above steps, print the elements of the stack Des as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void RecursiveCloneStack(stack<int>& S,
stack<int>& Des)
{
if (S.size() == 0)
return;
int val = S.top();
S.pop();
RecursiveCloneStack(S, Des);
Des.push(val);
}
void cloneStack(stack<int>& S)
{
stack<int> Des;
RecursiveCloneStack(S, Des);
cout << "Destination:- ";
int f = 0;
while (!Des.empty()) {
if (f == 0) {
cout << Des.top();
f = 1;
}
else
cout << " "
<< Des.top();
Des.pop();
cout << '\n';
}
}
int main()
{
stack<int> S;
S.push(1);
S.push(2);
S.push(3);
S.push(4);
S.push(5);
cloneStack(S);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static Stack<Integer> S = new Stack<Integer>();
static Stack<Integer> Des = new Stack<Integer>();
static void RecursiveCloneStack()
{
if (S.size() == 0)
return;
int val = S.peek();
S.pop();
RecursiveCloneStack();
Des.push(val);
}
static void cloneStack()
{
RecursiveCloneStack();
System.out.print("Destination:- ");
int f = 0;
while (Des.size() > 0) {
if (f == 0) {
System.out.print(Des.peek());
f = 1;
}
else
System.out.print(" " + Des.peek());
Des.pop();
System.out.println();
}
}
public static void main(String[] args) {
S.push(1);
S.push(2);
S.push(3);
S.push(4);
S.push(5);
cloneStack();
}
}
|
Python3
S = []
Des = []
def RecursiveCloneStack():
if (len(S) == 0):
return
val = S[-1]
S.pop()
RecursiveCloneStack()
Des.append(val)
def cloneStack():
RecursiveCloneStack()
print("Destination:- ", end = "")
f = 0
while len(Des) > 0:
if (f == 0):
print(Des[-1], end = "")
f = 1
else:
print(" ", Des[-1], end = "")
Des.pop()
print()
S.append(1)
S.append(2)
S.append(3)
S.append(4)
S.append(5)
cloneStack()
|
C#
using System;
using System.Collections;
class GFG {
static Stack S = new Stack();
static Stack Des = new Stack();
static void RecursiveCloneStack()
{
if (S.Count == 0)
return;
int val = (int)S.Peek();
S.Pop();
RecursiveCloneStack();
Des.Push(val);
}
static void cloneStack()
{
RecursiveCloneStack();
Console.Write("Destination:- ");
int f = 0;
while (Des.Count > 0) {
if (f == 0) {
Console.Write(Des.Peek());
f = 1;
}
else
Console.Write(" " + Des.Peek());
Des.Pop();
Console.WriteLine();
}
}
static void Main() {
S.Push(1);
S.Push(2);
S.Push(3);
S.Push(4);
S.Push(5);
cloneStack();
}
}
|
Javascript
<script>
S = [];
Des = [];
function RecursiveCloneStack()
{
if (S.length == 0)
return;
let val = S[S.length - 1];
S.pop();
RecursiveCloneStack();
Des.push(val);
}
function cloneStack()
{
RecursiveCloneStack();
document.write("Destination:- ");
let f = 0;
while (Des.length > 0) {
if (f == 0) {
document.write(Des[Des.length - 1]);
f = 1;
}
else{
document.write(" " + Des[Des.length - 1]);
}
Des.pop();
document.write("</br>");
}
}
S.push(1);
S.push(2);
S.push(3);
S.push(4);
S.push(5);
cloneStack();
</script>
|
Output:
Destination:- 5
4
3
2
1
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...