pwstrick / daily

一份搜集的前端面试题目清单、面试相关以及各类学习的资料(不局限于前端)
2.39k stars 242 forks source link

用两个栈实现队列 #1009

Open pwstrick opened 4 years ago

pwstrick commented 4 years ago
var arr1 = [],        //栈 A
  arr2 = [];        //栈 B
function push(node) {
  arr1.push(node);
}
function pop() {
  if (arr2.length > 0) {
    return arr2.pop();
  }
  while (arr1.length > 0) {
    arr2.push(arr1.pop());
  }
  return arr2.pop();
}