class Stack {
constructor() {
this.list = [];
}
push(value) {
this.list.push(value);
}
pop() {
return this.list.pop();
}
peek() {
return this.list[this.list.length - 1]
}
size() {
return this.list.length;
}
isEmpty() {
return this.list.length === 0;
}
clear() {
this.list = [];
}
toString() {
return this.list.join(",")
}
}
// 测试代码
const stack = new Stack();
stack.push(1)
stack.push(3)
stack.push(5)
stack.pop();
stack.push(7)
console.log(stack.peek())
console.log(stack.size())
console.log(stack.isEmpty())
console.log("toString =",stack.toString())
stack.clear();
console.log(stack.isEmpty())
console.log("toString =",stack.toString())