数据结构_双向循环链表_tostring方法

Posted junlan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构_双向循环链表_tostring方法相关的知识,希望对你有一定的参考价值。

//双向循环链表
  class DoubleNode {
    constructor (data) {
      this.data = data
      this.prev = null
      this.next = null
    }
  }
  class DoubleCycleList {
    constructor () {
      this.head = null
      this.tail = null
      this.length = 0
    }
    //追加数据
    append (data) {
      let newNode = new DoubleNode(data)
      let current = this.head
      //判断是否为空
      if (!this.head) {
        this.head = newNode
        this.tail = newNode
        this.head.prev = this.tail
        this.tail.next = this.head
      //否则在末尾添加数据
      }else {
        newNode.prev = this.tail
        newNode.next = this.head
        this.head.prev = newNode
        this.tail.next = newNode
        this.tail = newNode
      }
      this.length++
      return true
    }
    //插入数据
    insert (posi, data) {
      let newNode = new DoubleNode(data)
      let current = this.head
      let index = 0
      if (posi < 0 || posi > this.length || !Number.isInteger(posi)) return false
      if (posi == 0) {
        if (!this.head) {
          this.head = newNode
          this.tail = newNode
          this.head.prev = this.tail
          this.tail.next = this.head
        }else {
          newNode.next = this.head
          newNode.prev = this.tail
          this.head.prev = newNode
          this.tail.next = newNode
          this.head = newNode
        }
      }else if (posi == this.length) {
        newNode.prev = this.tail
        newNode.next = this.head
        this.head.prev = newNode
        this.tail.next = newNode
        this.tail = newNode
      }else {
        while (index++ < posi - 1) {
          current = current.next
        }
        newNode.next = current
        newNode.prev = current.prev
        current.prev.next = newNode
        current.prev = newNode
      }
      this.length++
      return true
    }
    //查询
    indexOf (data) {
      let current = this.head
      let index = 0
      while (current != this.tail) {
        if (current.data === data) {
          return index
        }else {
          index++
          current = current.next
        }
      }
      if (current.data === data) {
        return index
      }
      return -1
    }
    //删除指定位置
    removeAt (posi) {
      let current = this.head
      let index = 0
      if (posi < 0 || posi > this.length || !Number.isInteger(posi)) return false
      if (posi == 0) {
        if (this.length == 1) {
          this.head = null
          this.tail = null
        }else {
          this.head.next.prev = this.tail
          this.tail.next = this.head.next
          this.head = this.head.next
        }
      }else if (this.length - 1 == posi) {
        this.tail.prev.next = this.head
        this.head.prev = this.tail.prev
        this.tail = this.tail.prev
      }else {
        while (index++ < posi -1) {
          current = current.next
        }
        current.next = current.next.next
        current.next.prev = current
      }
      this.length--
      return true
    }
    remove (data) {
      return this.removeAt(this.indexOf(data))
    }
    isEmpty () {
      return this.head == null
    }
    size () {
      return this.length
    }
    tostring () {
      let current = this.head
      let re = ‘‘
      while (current != this.tail) {
        re += ‘,‘ + current.data
        current = current.next
      }
      re += ‘,‘+current.data
      return re.slice(1)
    }
  }
  let dList = new DoubleCycleList()
  dList.insert(0, 0)
  dList.append(1)
  dList.append(2)
  dList.append(3)
  dList.append(4)
  dList.insert(5, ‘number5‘)

 

以上是关于数据结构_双向循环链表_tostring方法的主要内容,如果未能解决你的问题,请参考以下文章

数据结构-内核的双向循环链表-简单实现

PHP双向循环链表代码

《链表》之带头双向循环链表

数据结构:如何用C语言快速实现带头双向循环链表

数据结构带头双向循环链表

数据结构与算法篇-双向循环链表