Skip to main content

用栈实现队列

Tips

题目类型: Design

相关题目:

题目

用栈实现队列

题解

/**
* Initialize your data structure here.
*/
var MyQueue = function () {
this.stack = []
}

MyQueue.prototype = {
get len() {
return this.stack.length
},
}

/**
* Push element x to the back of queue.
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function (x) {
this.stack = [x, ...this.stack]
}

/**
* Removes the element from in front of queue and returns that element.
* @return {number}
*/
MyQueue.prototype.pop = function () {
return this.stack.pop()
}

/**
* Get the front element.
* @return {number}
*/
MyQueue.prototype.peek = function () {
return this.stack[this.len - 1]
}

/**
* Returns whether the queue is empty.
* @return {boolean}
*/
MyQueue.prototype.empty = function () {
return !this.len
}

/**
* Your MyQueue object will be instantiated and called as such:
* var obj = new MyQueue()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.empty()
*/