【JS数据结构】栈

想刷leecode题来着 作为前端开发 肯定用的JS,但用JS又比之前用C刷题来的怪,就这样磨磨蹭蹭到了年底 现在就来学一遍Js的数据结构,学的舒心,刷的放心
  1. 直接上代码吧
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()

  1. 然后再上上 实例
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老师

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值