JavaScript算法系列之-----------------用两个栈实现队列(JS实现)

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
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
        }
        
    }
}

  

猜你喜欢

转载自www.cnblogs.com/manru75/p/10441512.html