class Stack {
//属性
constructor() {
this.items = [];
}
//压栈
push(ele) {
this.items.push(ele);
}
//弹栈
pop() {
return this.items.pop();
}
//peek
peek() {
return this.items[this.items.length - 1];
}
//判断是否为空
isEmpty() {
return this.items.length === 0;
}
//size
size() {
return this.items.length;
}
}
let s = new Stack();
s.push(3);
s.push(4);
s.pop();
console.log(s.peek());
console.log(s.isEmpty());
console.log(s.size());
js实现栈结构
最新推荐文章于 2022-04-10 19:56:36 发布