-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathReverse_Stack.java
42 lines (37 loc) · 964 Bytes
/
Reverse_Stack.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
import java.util.*;
public class Reverse_Stack {
//reverse a stack
public static void pushAtBottom(Stack<Integer> s, int data) {
// at every level -> upar jate hue element ko remove
if (s.isEmpty()) {
s.push(data);
return;
}
int top = s.pop();
pushAtBottom(s, data);
s.push(top);
}
public static void reverseStack(Stack<Integer> s){
if(s.isEmpty()){
return;
}
int top = s.pop(); //removing of top
reverseStack(s); //next call
pushAtBottom(s, top); //stack insert from bottom
}
public static void printStack(Stack<Integer> s){
while(!s.isEmpty()){
System.out.println(s.pop());
}
}
public static void main(String[] args) {
Stack<Integer> s= new Stack<>();
s.push(1);
s.push(2);
s.push(3);
// 3 2 1
reverseStack(s);
printStack(s);
// 1 2 3
}
}