概念
栈是一种线性结构,最大的特点就是先进后出,后进先出。
入栈push()
:
出栈pop()
:
实现
JavaScript中可以用数组表示栈:
class Stack {
constructor() {
this.stack = [];
}
push(item) {
this.stack.push(item)
}
pop() {
this.stack.pop()
}
getCount() {
return this.stack.length
}
peek() {
return this.stack[this.getCount() - 1]
}
isEmpty() {
return this.stack.length === 0
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)