栈、队列
栈: 弹夹,后进先出
队列: 排队,先进先出
描述:
var MyStack = function () {// 定义两个数组,模拟队列this.queue = []this._queue = []
};/** * @param {number} x* @return {void}*/
MyStack.prototype.push = function (x) {// 后插this.queue.push(x)
};/*** @return {number}*/
MyStack.prototype.pop = function () {// 返回栈顶元素,后进先出let answhile(this.queue.length > 1) {this._queue.push(this.queue.shift())}ans = this.queue.shift()while(this._queue.length){this.queue.push(this._queue.shift())}return ans
};/*** @return {number}*/
MyStack.prototype.top = function () {// 返回栈顶元素,return this.queue.slice(-1)[0]
};/*** @return {boolean}*/
MyStack.prototype.empty = function () {return !this.queue.length
};
用数组模拟栈、队列,没啥意思