首页 > 技术文章 > 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型

zhouyideboke 2019-07-08 15:09 原文

以下为借鉴

var stack1 = [], stack2=[];

function push(node){
    stack1.push(node);
}
function pop(){
    if(stack2.length){
        return stack2.pop();
    }else{
        if(stack1.length){
            var len = stack1.length;
            for(var i=0;i<len;i++){
                stack2.push(stack1.pop());
            }
            return stack2.pop()
        }else{
             return null
        }
         
    }
}
 
 

推荐阅读