package com.chanmufeng.codingInterviewGuide.stackAndQueue_10;
import java.util.Stack;
/**
* 用栈实现另一个栈的排序(由大到小)
*/
public class SortStackByStack {
public static void solve(Stack<Integer> stack) {
Stack<Integer> help = new Stack();
while (!stack.isEmpty()) {
int cur = stack.pop();
if (!help.isEmpty() && cur >= help.peek()) {
stack.push(help.pop());
}
help.push(cur);
}
while (!help.isEmpty()) {
stack.push(help.pop());
}
}
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(2);
stack.push(4);
stack.push(1);
stack.push(8);
stack.push(5);
stack.push(9);
stack.push(10);
solve(stack);
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}
}