Java Reference
In-Depth Information
this.peek = function () {
if (this.isEmpty()) {
throw new Error("Stack is empty.");
}
else {
return data[data.length - 1];
}
};
this.toString = function () {
return data.toString();
};
}
Listing 7-3. Testing the Stack Object
// stacktest.js
load("stack.js");
// Create a Stack with initial 2 elements
var stack = new Stack(10, 20);
print("Stack = " + stack);
// Push an element
stack.push(40);
print("After push(40), Stack = " + stack);
// Pop two elements
stack.pop();
stack.pop();
print("After 2 pops, Stack = " + stack);
print("stack.peek() = " + stack.peek());
print("stack.isEmpty() = " + stack.isEmpty());
// Pop the last element
stack.pop();
print("After another pop(), stack.isEmpty() = " + stack.isEmpty());
Search WWH ::




Custom Search