• 利用栈实现队列


    public class MyQueue {
    
        private Stack<Integer> stackPush;
        private Stack<Integer> stackPop;
    
        public MyQueue() {
            stackPush = new Stack<Integer>();
            stackPop = new Stack<Integer>();
        }
    
        public void push(int pushInt) {
            stackPush.push(pushInt);
            dump();
        }
    
        public int poll() {
            if (stackPop.empty() && stackPush.empty()) {
                throw new RuntimeException("Queue is empty!");
            }
            dump();
            return stackPop.pop();
        }
    
        public int peek() {
            if (stackPop.empty() && stackPush.empty()) {
                throw new RuntimeException("Queue is empty!");
            }
            dump();
            return stackPop.peek();
        }
    
        public void dump() {
            if (!stackPop.isEmpty()) {
                return;
            }
            while (!stackPush.isEmpty()) {
                stackPop.push(stackPush.pop());
            }
        }
    }
  • 相关阅读:
    Swift
    ios高质量博客
    Swift
    UML建模
    Swift
    Swift
    IIS建立.net framework4 应用程序池HTTP 错误 500.21
    zz entity framework vs linq to sql
    zz部署wcf iis
    zzIIS站点中部署WCF项目
  • 原文地址:https://www.cnblogs.com/moris5013/p/11627331.html
Copyright © 2020-2023  润新知