用一个栈实现另一个栈的排序

来源:互联网 发布:基三正太捏脸数据 编辑:程序博客网 时间:2024/04/26 23:04
/** * 用一个栈实现另一个栈的排序 * @author yyq * */public class StackSortDemo {    public void sortStackByStack(Stack<Integer> stack){        Stack<Integer> help = new Stack<>();        if(stack.isEmpty()){            throw new RuntimeException("stack is empty");        }        if(!stack.isEmpty()){            int value = stack.pop();            while(!help.isEmpty() && help.peek() > value){                stack.push(help.pop());            }            help.push(value);        }        while(!help.isEmpty()){            stack.push(help.pop());        }    }}
原创粉丝点击