想刷leecode题来着 作为前端开发 肯定用的JS,但用JS又比之前用C刷题来的怪,就这样磨磨蹭蹭到了年底 现在就来学一遍Js的数据结构,学的舒心,刷的放心
- 直接上代码吧
function Stack () {
this.item = []
Stack.prototype.push = function (element) {
this.item.push(element)
}
Stack.prototype.pop = function () {
this.item.pop()
}
Stack.prototype.peek = function () {
return this.item[this.item.length - 1]
}
Stack.prototype.size = function () {
return this.item.length
}
Stack.prototype.isEmpty = function () {
return this.item.length == 0
}
Stack.prototype.toString = function () {
var resultString = ''
for (let i = 0; i < this.item.length; i++) {
resultString += this.item[i] + ''
}
return resultString
}
}
var s = new Stack()
- 然后再上上 实例
s.push(1)
s.push(3)
s.push(5)
s.pop()
console.log('s.toString():', s.toString())
console.log('s.isEmpty:', s.isEmpty())
console.log(s.peek())
console.log(s)
师从coderwhy老师