数据结构_双向循环链表_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')
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!