1.push 进栈
2.pop 出栈
3.peek 获取栈顶
4.isEmpty 判断栈是否为空
5.size 栈的长度
class Stack {
constructor() {
this.items = [];
}
push(value) {
this.items.push(value)
}
pop() {
return this.items.pop()
}
peek() {
return this.items[this.items.length - 1]
}
isEmpty() {
return this.items.lehgth ==0;
}
size() {
return this.items.length
}
}
var s = new Stack();
s.push(2);
s.push(5);
s.push(8);
console.log(s);
s.pop();
console.log(s);
console.log(s.peek());//5
console.log(s.isEmpty());//false
console.log(s.size());//2