Skip to content

Latest commit

 

History

History
44 lines (29 loc) · 686 Bytes

用两个栈实现队列.md

File metadata and controls

44 lines (29 loc) · 686 Bytes

题目

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

思路

栈1:

用于入队列存储

栈2:

出队列时将栈1的数据依次出栈,并入栈到栈2中

栈2出栈即栈1的底部数据即队列要出的数据。

注意:

栈2为空才能补充栈1的数据,否则会打乱当前的顺序。

代码

const stack1 = [];
const stack2 = [];

function push(node)
{
    stack1.push(node);
}
function pop()
{
    if(stack2.length === 0){
       while(stack1.length>0){
        stack2.push(stack1.pop());
       }
    }
    return stack2.pop() || null;
}