在 JavaScript 中实现堆栈和队列的最佳方式是什么?
我正在寻找调车场算法,我需要这些数据结构。
最佳答案
var stack = [];
stack.push(2); // stack is now [2]
stack.push(5); // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i); // displays 5
var queue = [];
queue.push(2); // queue is now [2]
queue.push(5); // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i); // displays 2
关于javascript - 如何在 JavaScript 中实现堆栈和队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1590247/