栈实现队列

因为这个是自己写的,可能很麻烦,不过特别好懂


import java.util.Stack;

public class 队列实现栈 {

    private static Stack<Object> stackin = new Stack<>();
    private static Stack<Object> stackout = new Stack<>();

    public void push(int x){
        stackin.push(x);
    }

    public  Object pop(){
        for (int i = 0; i < stackin.size(); i++) {
            stackout.push(stackin.pop());
        }
        Object i = stackout.pop();
        for (int j = 0; j < stackout.size(); j++) {
            stackin.push(stackout.pop());
        }
        return i;
    }

    public Boolean empty(){
        return stackout.empty()&&stackin.empty();
    }
}

 

posted @ 2022-05-30 08:37  七色angel  阅读(14)  评论(0编辑  收藏  举报