数据结构与算法(栈和队列) --javascript语言描述
用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
var stack1 = []; var stack2 = []; function push(node){ stack1.push(node); } function pop(){ if(stack2.length <= 0) { while(stack1.length > 0) { let data = stack1.pop(); stack2.push(data); } } return stack2.pop(); } push(1); push(2); push(3); push(4); console.log(pop()); console.log(pop()); console.log(pop()); console.log(pop());
相关推荐
shenwenjie 2020-09-24
xiesheng 2020-08-02
mingyunxiaohai 2020-07-28
Cypress 2020-07-08
Cypress 2020-06-28
Dyancsdn 2020-06-27
Masimaro 2020-06-21
waitwolf 2020-06-21
Jasmineyaoyao 2020-06-16
OldBowl 2020-06-16
alicelmx 2020-06-16
Masimaro 2020-06-14
waitwolf 2020-06-08
nurvnurv 2020-06-05
ustbfym 2020-06-04
ziruoxiangyi 2020-06-03
范范 2020-06-03
Jasmineyaoyao 2020-05-31