-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseStack.java
More file actions
68 lines (55 loc) · 1.2 KB
/
ReverseStack.java
File metadata and controls
68 lines (55 loc) · 1.2 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
68
package com.duke.stack;
import java.util.Stack;
public class ReverseStack {
private void reverse(Stack<Integer> stack) {
if (!stack.isEmpty()) {
int x = stack.pop();
reverse(stack);
insert_reverse_stack(stack, x);
}
// System.out.println(stack);
}
private void insert_reverse_stack(Stack<Integer> stack, int x) {
if (stack.isEmpty()) {
stack.push(x);
} else {
int y = stack.pop();
insert_reverse_stack(stack, x);
stack.push(y);
}
}
public static void main(String[] args) {
Stack<Integer> st = new Stack();
st.push(1);
st.push(2);
st.push(3);
st.push(4);
ReverseStack o = new ReverseStack();
o.reverse(st);
System.out.println(st);
Stack<Integer> st1 = new Stack();
st1.push(30);
st1.push(-5);
st1.push(18);
st1.push(14);
st1.push(-3);
o.sort(st1);
System.out.println(st1);
}
private void sort(Stack<Integer> st) {
if (!st.isEmpty()) {
int x = st.pop();
sort(st);
sortStack(st, x);
}
}
private void sortStack(Stack<Integer> st, int x) {
if (st.empty() || st.peek() < x) {
st.push(x);
} else {
int y = st.pop();
sortStack(st, x);
st.push(y);
}
}
}