02用队列实现栈

使用队列实现栈的下列操作:

push(x) -- 元素 x 入栈
pop() -- 移除栈顶元素
top() -- 获取栈顶元素
empty() -- 返回栈是否为空

题目来源:力扣(LeetCode)

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

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

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

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

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

/**
 * 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()
 */

你可能感兴趣的:(算法,栈)