-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackJCF.java
61 lines (56 loc) · 1.59 KB
/
StackJCF.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
55
56
57
58
59
60
61
import java.util.*;
public class StackJCF {
public static void print(Stack<Integer> s) {
while (!s.isEmpty()) {
System.out.println(s.peek());
s.pop();
}
}
public static void pushToBottom(Stack<Integer> s, int data) {
if (s.isEmpty()) {
s.push(data);
return;
}
int top = s.pop();
pushToBottom(s, data);
s.push(top);
}
//reverse string
public static void reverseString(String str) {
Stack<Character> s = new Stack<>();
int index = 0;
// from string's index 0 to n we push string in stack
while (index < str.length()) {
s.push(str.charAt(index));
index++;
}
// to reverse string in faster way we use stringbuilder
StringBuilder result = new StringBuilder("");
while (!s.isEmpty()) {
char curr = s.pop();
result.append(curr);
}
str = result.toString(); // toString function coz type of string and stringbuilder is different.
System.out.println(str);
}
//reverse a stack
public static void reverseStack(Stack<Integer> s) {
if (s.isEmpty()) {
return;
}
int top = s.pop();
reverseStack(s);
pushToBottom(s, top);
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
s.push(1);
s.push(2);
s.push(3);
pushToBottom(s, 4);
String str = "abcdefg";
reverseString(str);
reverseStack(s);
print(s);
}
}