[Cattle off network - to prove safety offer] queue with two stacks

topic:

Two stacks to achieve a queue, the completion queue Push and Pop operations. Queue elements int.

Knowledge and concepts:

Queue: Queue is a special kind of linear form, it is special in that it only allows deletion at the front end of the table (Front), while the rear end of insertion, the table (REAR), and stack as a queue restricted operation of the linear table. Referred to as the tail end of the end, delete operation will be referred to as head-of-insertion operation.
Stack:

push () and pop ()

Ideas:

Code:

var stack1=[],stack2=[];

function push(node)
{
    // write code here
    stack1.push(node);
}
function pop()
{
    if(stack2.length==0){
        if(stack1.length==0){
            return null;
        }else{
            var len = stack1.length;
            for(var i=0;i<len;i++){
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }else{
        return stack2.pop();
    }
  
}

Guess you like

Origin www.cnblogs.com/xiakecp/p/11574646.html