-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackQ2.java
More file actions
36 lines (31 loc) · 784 Bytes
/
StackQ2.java
File metadata and controls
36 lines (31 loc) · 784 Bytes
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
import java.util.*;
//Code to Reverse a Stack
// Using collection framework
public class StackQ2{
public static void pushAtBottom(Stack<Integer> s, int data) {
if(s.isEmpty()) {
s.push(data);
return;
}
int temp = s.pop();
pushAtBottom(s, data);
s.push(temp);
}
public static void reverse(Stack<Integer> s) {
if(s.isEmpty()) {
return;
}
int top = s.pop();
reverse(s);
pushAtBottom(s, top);
}
public static void main(String args[]) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
}