-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloneStack.java
68 lines (54 loc) · 1.62 KB
/
CloneStack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package Stack;
public class CloneStack {
static Stack<Integer> S = new Stack<Integer>();
static Stack<Integer> Des = new Stack<Integer>(); // Stores the destination stack
// Auxiliary function to copy elements
// of source stack to destination stack
static void RecursiveCloneStack()
{
// Base case for Recursion
if (S.size() == 0)
return;
// Stores the top element of the
// source stack
int val = S.peek();
// Removes the top element of the
// source stack
S.pop();
// Recursive call to the function
// with remaining stack
RecursiveCloneStack();
// Push the top element of the source
// stack into the Destination stack
Des.push(val);
}
static void cloneStack()
{
// Recursive function call to copy
// the source stack to the
// destination stack
RecursiveCloneStack();
System.out.print("Destination:- ");
int f = 0;
// Iterate until stack Des is
// non-empty
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();
}
}