-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_stack.java
More file actions
68 lines (52 loc) · 1.31 KB
/
Solution_stack.java
File metadata and controls
68 lines (52 loc) · 1.31 KB
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
import java.util.Stack;
public class Solution_stack {
public static void push_at_bottom(Stack<Integer> st , int data){
if(st.empty()){
st.push(data);
return;
}
int top = st.pop();
push_at_bottom(st,data);
st.push(top);
}
public static String rev_using_stack(String str){
Stack<Character> s = new Stack<>();
int index = 0;
int n = str.length();
while (index<n) {
s.push(str.charAt(index));
index++;
}
StringBuilder sb = new StringBuilder("");
while (!s.empty()) {
char st = s.pop();
sb.append(st);
}
return sb.toString();
}
public static void rev_stack(Stack<Integer> s){
if(s.empty()){
return;
}
int top = s.pop();
rev_stack(s);
push_at_bottom(s,top);
}
public static void main(String[] args){
Stack<Integer> st = new Stack<Integer>();
st.push(1);
st.push(2);
st.push(3);
push_at_bottom(st,4);
while(!st.empty()){
System.out.println(st.pop());
}
String s = "Hello";
String str_1 = rev_using_stack(s);
System.out.println(str_1);
rev_stack(st);
while (st.empty()) {
System.out.println(st.pop());
}
}
}