// 栈类
class Stack {
constructor() {
this.items = [];
}
//往栈中添加元素
push(value) {
this.items.push(value);
}
// 删除栈顶元素
pop(value) {
return this.items.pop(value);
}
// 查看栈顶元素
peek() {
return this.items[this.items.length - 1];
}
// 判断栈是否为空
isEmpty() {
return this.items.length == 0;
}
// 查看栈的长度
size() {
return this.items.length;
}
// 将栈转成字符串
toString() {
return this.items.join("");
}
}
JavaScript栈类的封装
最新推荐文章于 2024-06-09 20:17:27 发布