简介
1 .栈内的元素只能通过列表的一端访问,栈顶
2 .先入后出
3 .任何不在栈顶的元素都无法访问
class Stack{
constructor(){
this.dataStore=[]
this.top=0
}
push(el){
return this.dataStore[this.top++]=el
}
top(){
return this.dataStore[--this.top]
}
clear(){
this.dataStore=[]
return this.top=0
}
length(){
return this.top
}
}
var s = new Stack();
s.push("David");
s.push("Raymond");
s.push("Bryan");
console.log(s.length())
s.clear();
console.log(s.length());
网友评论