-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortStackByStack.java
More file actions
48 lines (36 loc) · 974 Bytes
/
SortStackByStack.java
File metadata and controls
48 lines (36 loc) · 974 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package stackAndqueue;
import org.junit.Test;
import java.util.Stack;
/**
* Created by LXF on 2016/7/25.
*/
public class SortStackByStack {
public SortStackByStack() {
}
public static void SortSactkByStackC(Stack<Integer> stack) {
Stack<Integer> help = new Stack<Integer>();
while(!stack.isEmpty()) {
int cur = stack.pop();
while(!help.isEmpty() && help.peek() < cur) {
stack.push(help.pop());
}
help.push(cur);
}
while(!help.isEmpty()) {
stack.push(help.pop());
}
}
@Test
public void test() {
Stack stack = new Stack();
stack.push(3);
stack.push(6);
stack.push(7);
stack.push(53);
SortStackByStack sortSackByStack = new SortStackByStack();
SortSactkByStackC(stack);
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
}