AwesomeDevin / blog

Welcome to Devin's blog,I'm trying to be a fullstack developer and sticking with it !!!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

【算法系列 - 剑指Offer】实现队列

AwesomeDevin opened this issue · comments

题目1:

实现1个队列

JS实现

const stack1 = []
function push(node)
{
    stack1.push(node)
    // write code here
}
function pop()
{
    return stack1.shift()
    // write code here
}

题目2:

用两个栈实现1个队列

JS实现

var stack1 = []
var stack2 = []
function push(node)
{
    stack1.push(node)
    // write code here
}
function pop()
{
    while(stack1.length>1)
    {
         stack2.push(stack1.pop())
    }
    var res = stack1[0]
    stack1 = stack2.reverse()
    stack2 = []
    return res
    // write code here
}