JavaScript算法试题系列-06 发表于 2017-06-15 | 更新于 2017-06-15 | 分类于 学习笔记 , JavaScript | | 共 111 字 | 阅读 次 题目要求: 1234567// 栈与队列使用两个栈实现入队与出队var inputStack = []; // First stackvar outputStack = []; // Second stack// For enqueue, just push the item into the first stackfunction enqueue(stackInput, item) { return stackInput.push(item);} 解答: 12345678910111213function dequeue(stackInput, stackOutput) { // Reverse the stack such that the first element of the output stack is the // last element of the input stack. After that, pop the top of the output to // get the first element that was ever pushed into the input stack if (stackOutput.length <= 0) { while(stackInput.length > 0) { var elementToOutput = stackInput.pop(); stackOutput.push(elementToOutput); } } return stackOutput.pop();} ------ 本文结束 ------ 版权声明 加州提子面包 by MiG is licensed under a Creative Commons BY-NC-SA 4.0 International License. 由MiG创作并维护的加州提子面包博客采用「创作共用保留署名-非商业性使用-相同方式共享」4.0国际许可证. 本文首发于加州提子面包 博客( https://gihcctpd.github.io ),版权所有,侵权必究.