当前位置:首页 » 《随便一记》 » 正文

JavaScript数据结构双向链表的封装_weiyuyang250的博客

3 人参与  2022年05月22日 09:10  分类 : 《随便一记》  评论

点击全文阅读


    function DoublyLinkedList() {
      function Node(data){
        this.next = null
        this.prev = null
        this.data = data
      }
      this.head = null
      this.tail = null
      this.length = 0
      DoublyLinkedList.prototype.append = function(data) {
        var newNode = new Node(data)
        if (this.length === 0){
          this.head = newNode
          this.tail = newNode
        }else{
          newNode.prev = this.tail
          this.tail.next = newNode
          this.tail = newNode
        }
        this.length++
      }
      DoublyLinkedList.prototype.toString = function() {
        return this.forwardString()
      }
      DoublyLinkedList.prototype.backwardString = function() {
        var current = this.tail
        var listString = ''
        while(current) {
          listString += current.data + ''
          current = current.prev
        }
        return listString
      }
      DoublyLinkedList.prototype.forwardString = function() {
        var current = this.head
        var listString = ''
        while (current) {
          listString += current.data + ''
          current = current.next
        }
        return listString
      }
      DoublyLinkedList.prototype.insert = function(data, position) {
        if (position < 0 || position > this.length) return false
        var newNode = new Node(data)
        if (this.length === 0) {
          this.head = newNode
          this.tail = newNode
        }else{
          if (position === 0) {
            newNode.next = this.head
            this.head.prev = newNode 
            this.head = newNode
          }else if(position == this.length){
            newNode.prev = this.tail
            this.tail.next = newNode
            this.tail = newNode
          }
          else {
            var current = this.head
            var previous = null
            var index = 0
            while(index++ < position){
              previous = current
              current = current.next
            }
            newNode.next = current
            current.prev = newNode
            previous.next = newNode
            newNode.prev = previous
          }
        }
        this.length++
      }
      DoublyLinkedList.prototype.get = function(position) {
        if (position < 0 || position >= this.length) return null
        var index = 0
        var current = this.head
        while (index++ < position) {
          current = current.next
        }
        return current
      }
      DoublyLinkedList.prototype.indexOf = function(data) {
        var index = 0
        var current = this.head
        while(current) {
          if (current.data === data) {
            return index
          }
          current = current.next
          index++
        }
        return -1
      }
      DoublyLinkedList.prototype.update = function(data, position) {
        if (position < 0 || position >= this.length) return false
        var index = 0
        var current = this.head
        while (index++ < position) {
          current = current.next
        }
        current.data = data
        return true
      }
      DoublyLinkedList.prototype.removeAt = function (position) {
        if (position < 0 || position >= this.length) return null
        var index = 0
        var current = this.head
        var previous = null
        if (this.length === 1) {
          this.head = null
          this.tail = null
        }else{
          if (position === 0) {
            this.head = this.head.next
            this.head.next.prev = null
          }else if(position == this.length-1) {
            current = this.tail
            this.tail = this.tail.prev
            this.tail.prev.next = null
          }else {
            while (index++ < position) {
              current = current.next
            }
            current.prev.next = current.next
            current.next.prev = current.prev
          }
        }
        this.length--
        return current.data
      }
      DoublyLinkedList.prototype.remove = function(data) {
        var position = this.indexOf(data)
        return this.removeAt(position)
      }
      DoublyLinkedList.prototype.isEmpty = function() {
        if (this.length) return true
        return false
      }
      DoublyLinkedList.prototype.size = function() {
        return this.length
      }
    }


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/40574.html

append  var  length  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 落青梅,佛前几度相思泪:+后续***(宋佳音陆景琛)终章阅读无广告
  • 很快,他们都自由了:结局+番外新上热文(叶浅秋陆凌川)最近章节列表
  • 养妹被豪门认回后做成女体盛,缅北杀神的我杀疯了完结全文_妹妹苏月月宾客优质全文_小说后续在线阅读_无删减免费完结_
  • 京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸列表_京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸(许知意顾云夕)
  • (书荒必看)夏晚歌陆秋震惊!玄学大佬被读心了:+后续热血十足
  • 老公逼我给女大献血补身后,他悔疯了前言+后续(沈佳音顾时安)_老公逼我给女大献血补身后,他悔疯了前言+后续
  • 攻略对象害我绝嗣后,她悔疯了节选_林知夏阿宇方宇热文_小说后续在线阅读_无删减免费完结_
  • 如愿娶到假巫女养妹,他却悔疯了超长版_渺渺洛景修沈含霜连载_小说后续在线阅读_无删减免费完结_
  • 完结文京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸完结列表_完结文京圈太子爷扫黄被抓,我改嫁八块腹肌风流俏奶爸完结(许知意顾云夕)
  • 韩忆雪顾晏州小说(离婚吧顾总,这豪门怨妇我不当了)前传+全书阅读新作预览
  • 掌握家庭小荷包后,我倒欠十万块免费林蕊秦川完本_掌握家庭小荷包后,我倒欠十万块免费(林蕊秦川)
  • 班花的早餐奶一口气读完(林夏秦诗诗陆行止)_班花的早餐奶一口气读完

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1