试图找到这个堆栈的最小值;但是,每当我在 JSFiddle 中运行它时,什么都不会打印出来……有人向我解释为什么?这是代码:
function min_stack() {
var min = 0;
this.elements = [];
this.push = function(element) {
this.elements.push(element);
}
this.pop = function() {
return this.elements.pop();
}
this.min = function() {
min = this.elements[0];
if (this.elements.length > 0) {
for(int i = 0; i < this.elements.length; i++) {
if (min > this.elements[i]) {
min = this.elements[i];
}
}
}
return min;
}
}
var myStack = new min_stack();
myStack.push(5);
myStack.push(4);
myStack.push(3);
print("[" + myStack.elements + "]");
print("min:" + myStack.min());
myStack.pop();
print("[" + myStack.elements + "]");
print("min:" + myStack.min());
myStack.pop();
print("[" + myStack.elements + "]");
print("min:" + myStack.min());