栈的实现
class Stack {
constructor() {
this.count = 0
this.data = []
}
push(item) {
this.data[this.count] = item
this.count ++
}
pop() {
if(isEmpty()) {
return
}
const temp = this.data[this.count -1 ]
delete this.data[--this.count]
return temp
}
clear() {
this.count = 0
this.data = []
}
size() {
return this.count
}
top() {
if(this.isEmpty()) {
return
}
return this.data[this.count - 1]
}
isEmpty() {
return this.count === 0
}
}
const s = new Stack()
s.push('a')
数据结构-栈
最新推荐文章于 2024-06-15 06:34:43 发布