剑指offer-05-用两个栈实现队列

题目描述

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

题目分析

栈:先入后出,队列:先入先出;可以通过两个栈模拟队列的先入先出效果。

代码

var inStack = [],outStack = [];
function push(node)
{
    // write code here
    return inStack.push(node);
}
function pop()
{
    // write code here
    if(!outStack.length){    //outStack为空才再次入栈
        while(inStack.length){
            outStack.push(inStack.pop());
        }
    }    
    return outStack.pop();
}
posted @ 2018-09-09 15:28  Shinea_SYR  阅读(86)  评论(0编辑  收藏  举报