class Stack {
constructor() {
this.dataStore = []; //{2}
this.top = 0;
}
//Stack方法
push(item) {
this.dataStore.push(item);
this.top++;
// this.dataStore[this.top++] = item;
}
pop() {
// return this.dataStore[--this.top];
this.top--;
return this.dataStore.pop();
}
peek() {
return this.dataStore[this.top - 1];
}
size() {
return this.top;
}
clear() {
this.dataStore = [];
}
}
[数据结构与算法]用ES6实现一个栈,class Stack
最新推荐文章于 2023-02-06 13:00:00 发布
本文详细介绍了如何利用ES6的class语法来实现一个基本的栈数据结构,包括push和pop等核心操作,帮助理解数据结构在JavaScript中的应用。

1281

被折叠的 条评论
为什么被折叠?



