-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practice12.java
54 lines (40 loc) · 1010 Bytes
/
Practice12.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
package Stack;
import java.util.Scanner;
import java.util.Stack;
public class Practice12 {
public static void pushAtBottom(Stack<Integer> st , int data){
if(st.isEmpty()){
st.push(data);
return;
}
int top = st.pop();
pushAtBottom(st, data);
st.push(top);
}
public static void reverse(Stack<Integer> st){
if(st.isEmpty()){
return;
}
int top = st.pop();
reverse(st);
pushAtBottom(st, top);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack<Integer> st = new Stack<>();
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
st.push(7);
st.push(8);
int data = 9;
reverse(st);
while (!st.isEmpty()) {
System.out.print(st.pop() + " ");
}
System.out.println();
}
}