
var MyStack = function() {
this.stack = [];
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
return this.stack.push(x);
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.stack.pop();
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
return this.stack[this.stack.length - 1];
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.stack.length === 0;
};
/**
* 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()
*/

本文介绍了如何使用JavaScript创建一个名为MyStack的类,包含了push(入栈)、pop(出栈)、top(查看栈顶元素)和empty(判断栈是否为空)的方法,展示了基本的栈数据结构操作。
1467

被折叠的 条评论
为什么被折叠?



