栈是一种遵从后进先出(LIFO)原则的有序集合。新添加或待删除的元素都保存在栈的同一端,称作栈顶,另一端就叫栈底。在栈里,新元素都靠近栈顶,旧元素都接近栈底。
class Stack{
constructor() {
this.items = [];
}
push(val) {
this.items.push(val);
}
pop() {
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
get size() {
return this.items.length;
}
clear() {
this.items = [];
}
get isEmpty() {
return this.items.length === 0;
}
}
const stack = new Stack();
stack.push(1)
console.log(stack.isEmpty)