数据结构_双向循环链表_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‘)

原文地址:https://www.cnblogs.com/JunLan/p/12354422.html

时间: 2024-08-11 16:14:31

数据结构_双向循环链表_tostring方法的相关文章

数据结构_线性表_链式存储_双向循环链表的基本操作

//双向链表,将头结点和尾结点链接起来,就构成了双向循环链表 //双向循环链表是将头结点的前驱指针指向了尾结点,同时将尾结点的后劲指针指向了头结点. //空表,头结点的前驱和后继指针均指向了自己,这也是判断双向循环链表是否为空的条件, //双向循环链表具有对称性 //缺点,是要付出空间代价的 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.一般我们都构造双向循环链表. 代

C语言通用双向循环链表操作函数集

说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低.     可基于该函数集方便地构造栈或队列集.     本函数集暂未考虑并发保护. 一  概念 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序通过链表中的指针链接次序实现.链表由一系列存储结点组成,结点可在运行时动态生成.每个结点均由两部分组成,即存储数据元素的数据域和存储相邻结点地址的指针域.当进行插入或删除操作时,链表只需修改相关结点的指针域即可,因此相比线性

小猪的数据结构辅助教程——2.7 线性表中的双向循环链表

小猪的数据结构辅助教程--2.7 线性表中的双向循环链表 标签(空格分隔): 数据结构 本节学习路线图与学习要点 学习要点: 1.了解引入双向循环链表的原因 2.熟悉双向循环链表的特点以及存储结构 3.掌握双向循环链表的一些基本操作的实现逻辑 4.掌握逆序输出双向循环链表元素逻辑 1.双向循环链表的引入 2.双向循环链表的存储结构 双向循环链表的特点: 上面也说了,空间换时间,比起循环链表只是多了一个指向前驱的指针 特点的话: 判断空表:L ->next = L -> prior = L; 存

数据结构8: 双向链表(双向循环链表)的建立及C语言实现

之前接触到的链表都只有一个指针,指向直接后继,整个链表只能单方向从表头访问到表尾,这种结构的链表统称为 “单向链表”或“单链表”. 如果算法中需要频繁地找某结点的前趋结点,单链表的解决方式是遍历整个链表,增加算法的时间复杂度,影响整体效率.为了快速便捷地解决这类问题,在单向链表的基础上,给各个结点额外配备一个指针变量,用于指向每个结点的直接前趋元素.这样的链表被称为“双向链表”或者“双链表”. 双链表中的结点 双向链表中的结点有两个指针域,一个指向直接前趋,一个指向直接后继.(链表中第一个结点的

数据结构之双向链表(包含双向循环链表)

双向(循环)链表是线性表的链式存储结构的又一种形式. 在之前已经讲述了单向链表和循环链表.相比于单向链表只能从头结点出发遍历整个链表的局限性,循环链表使得可以从任意一个结点遍历整个链表. 但是,不管单向链表也好,循环链表也罢,都只能从一个方向遍历链表,即只能查找结点的下一个结点(后继结点),而不能查找结点的上一个结点(前驱结点).鉴于上述问题,引入了双向链表.由于双向循环链表包含双向链表的所有功能操作.因此,我们只讲述双向循环链表. 与单向链表不同,双向链表的结点构造如下图所示.即一个结点由三个

数据结构-双向循环链表(无头结点)相关算法

#include <stdio.h>#include <stdlib.h>#define OVERFLOW -2#define OK 1#define ERROR 0 //此双向循环链表无头结点typedef int ElemType;typedef struct DulNode {    ElemType data;    struct DulNode *prior;    struct DulNode *next;}DulNode,*DulLinkList; DulLinkLi

数据结构基础(12) --双向循环链表的设计与实现

双向链表的操作特点: (1) "查询" 和单链表相同; (2)"插入" 和"删除"时需要同时修改两个方向上的指针. 但是对于双向循环链表则在表尾插入非常的迅速, 只需O(1)的时间,因为有指向前面的指针, 因此双向循环链表会很容易的找到位于表尾的元素,因此双向循环链表比较适用于频繁在表尾插入的情况. 空链表: 双向循环链表节点构造: class DoubleListNode { private: Type data; DoubleListNode

数据结构与算法——线性表链式存储(双向循环链表)

今天总结线性表中的双向循环链表. 什么是双向循环链表? 看名字估计也就知道了,首相他是一个循环链表,也就是最后一个结点的指针域不为空,而是指向头结点,其次与单向循环链表相比,它是双向的.所谓双向,就是给每个结点再增加一个指针域,这个指针域指向前一个结点. 即是下面这样(来自百度图片): 为什么要用双向循环链表? 无论单链表还是单向循环链表,都只有一个指针域,它们都是直接指向后继结点的,如果要查找当前结点的后继结点,会很方便.但是如果给定一个结点,要得到它的前继结点,就会很麻烦,必须从第一个元素开

数据结构开发(11):双向循环链表的实现

0.目录 1.双向循环链表的实现 2.小结 1.双向循环链表的实现 本节目标: 使用 Linux 内核链表实现 StLib 中的双向循环链表 template <typename T> class DualCircleList; StLib 中双向循环链表的设计思路: 数据结点之间在逻辑上构成双向循环链表,头结点仅用于结点的定位. 实现思路: 通过模板定义 DualCircleList 类,继承自 DualLinkList 类 在 DualCircleList 内部使用Linux内核链表进行实