剑指offer

05-用两个栈实现队列

2020-04-25  本文已影响0人  马甲要掉了

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

代码


const stackIn = [];
const stackOut = [];

function push(node)
{
    // write code here
    stackIn.push(node);
}
function pop()
{
    // write code here
    if(!stackOut.length){
        while(stackIn.length){
        stackOut.push(stackIn.pop());
        }
    }
        
    
    return stackOut.pop();
    
    
}
上一篇 下一篇

猜你喜欢

热点阅读