zoukankan      html  css  js  c++  java
  • 用两个栈实现队列,剑指offer P59

    public class QueueByStack {
        private Stack<Integer> stack1;
        private Stack<Integer> stack2;
        public QueueByStack() {
            // TODO Auto-generated constructor stub
            stack1 = new Stack<Integer>();
            stack2 = new Stack<Integer>();
        }
        
        public void appendTail(Integer a) {
            stack1.push(a);
        }
        public Integer deleteHead() throws Exception{
            if(stack2.isEmpty()) {
                while(!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            if(stack2.isEmpty()) {
                throw new Exception("队列为空,不能删除");
            }
            return stack2.pop();
        }
        public static void main(String[] args) throws Exception {
            new QueueByStack();
            QueueByStack ququeByStack = new QueueByStack();
            ququeByStack.appendTail(1);
            ququeByStack.appendTail(2);
            System.out.println(ququeByStack.deleteHead());
        }
        
        
    
    }
  • 相关阅读:
    关于vue的npm run dev和npm run build
    移动端meta行大全
    浅谈前端三大框架Angular、react、vue
    Web Workers
    Meta(其他信息)
    页面
    页面
    日期和时间
    ECharts教程(未完)
    页面
  • 原文地址:https://www.cnblogs.com/leehfly/p/5228626.html
Copyright © 2011-2022 走看看