// 栈是先进后出 function Stack() { this.arr = []; // push方法是把数据放入栈中 this.push = function (value) { this.arr.push(value); } // pop 是取数组的最后一个数据,从而实现先进后出 this.pop = function () { this.arr.pop(); } } var stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); console.log(stack.arr); stack.pop(); console.log(stack.arr); 结果如下:
// 队列 先进先出 function Queue(){ this.arr = []; // 把数据放入队列中 this.push = function (value) { this.arr.push(value); } // 从队列中取出队列中的第一个数据 this.pop = function () { this.arr.shift(); } } var queue = new Queue(); queue.push(1); queue.push(2); queue.push(3); console.log(queue.arr); queue.pop(); console.log(queue.arr); 结果如下: