Skip to main content

用队列实现栈

Tips

题目类型: Design

相关题目:

题目

用队列实现栈.

题解

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

MyStack.prototype = {
get len() {
return this.queue.length
},
}

/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function (x) {
this.queue.push(x)
}

/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function () {
const last = this.queue[this.len - 1]
this.queue.length = this.len - 1
return last
}

/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function () {
return this.queue[this.len - 1]
}

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

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