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

范范 2020-02-23

//双向循环链表
  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‘)

相关推荐