-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseStack.java
More file actions
34 lines (31 loc) · 778 Bytes
/
reverseStack.java
File metadata and controls
34 lines (31 loc) · 778 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
import java.util.Stack;
public class reverseStack {
public static void insertAtBottom(Stack<Integer> st, int x) {
if (st.size() == 0) {
st.push(x);
return;
}
int top = st.pop();
insertAtBottom(st, x);
st.push(top);
}
public static void reverse(Stack<Integer> st) {
if (st.size() == 1) {
return;
}
int top = st.pop();
reverse(st);
insertAtBottom(st, top);
}
public static void main(String[] args) {
Stack<Integer> st = new Stack<>();
st.push(10);
st.push(20);
st.push(30);
st.push(40);
st.push(50);
System.out.println(st);
reverse(st);
System.out.println(st);
}
}