-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortStackByStack.java
More file actions
46 lines (38 loc) · 938 Bytes
/
Copy pathSortStackByStack.java
File metadata and controls
46 lines (38 loc) · 938 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
/*
* 题目:仅使用一个堆栈和变量对另外一个堆栈进行排序
* 要求从栈底到栈顶从小到大开始排序
*/
package com.StackAndQueue;
import java.util.Stack;
public class SortStackByStack {
public SortStackByStack(Stack<Integer> s) {
// TODO Auto-generated constructor stub
Stack<Integer> help=new Stack<Integer>();
while(!s.isEmpty()){
int cur=s.pop();
if(help.isEmpty() || cur<=help.peek()) {
help.add(cur);
}
else {
while( !help.isEmpty() && cur>help.peek()) {
s.push(help.pop());
}
help.add(cur);
}
}
while(!help.isEmpty()) {
s.push(help.pop());
}
while(!s.isEmpty()) {
System.out.print(s.pop()+" ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<Integer> s1=new Stack<Integer>();
for(int j=20;j>0;j--) {
s1.push(j);
}
SortStackByStack a=new SortStackByStack(s1);
}
}