当前位置: 首页 > news >正文

使用js封装一个循环链表

直接不说废话,直接上代码,这里继承了单向链表的类LinkList ,可以查看之前的文章,点这里

class Node {constructor(element) {this.element = element;this.next = null;}
}class CirularLinkedList extends LinkList {constructor() {super();}push(element) {const node = new Node(element);let current;if (this.head === null) {this.head = node;} else {current = this.getNodeAt(this.size() - 1);console.log(current);current.next = node;node.next = this.head;}node.next = this.head;this.count++;}insert(element, index) {if (index >= 0 && index <= this.count) {const node = new Node(element);let current = this.head;if (index === 0) {if (this.head === null) {this.head = node;node.next = this.head;} else {node.next = current;current = this.getNodeAt(this.size() - 1);this.head = node;console.log(current);current.next = this.head;}} else {const previous = this.getNodeAt(index - 1);node.next = previous.next;previous.next = node;}this.count++;return true}return false}removeAt(index) {if (index >= 0 && index < this.count) {let current = this.head;if (index === 0) {if (this.size() === 0) {this.head = undefined;} else {let last = this.getNodeAt(this.size() - 1);this.head = this.head.next;}} else {const previous = thsi.getNodeAt(index - 1);current = previous.next;previous.next = current;}}}
}

http://www.lryc.cn/news/58441.html

相关文章:

  • NumPy 秘籍中文第二版:二、高级索引和数组概念
  • 新品-图灵超频工作站GT430M介绍
  • js时间格式化精确到毫秒
  • QT样式表详解
  • 最值得入手的好物有哪些,推荐几款实用的数码好物
  • 【20230407】NVIDIA显卡算力、Jetson比较
  • dsl语法
  • 不让CPU偷懒
  • 动力节点王鹤SpringBoot3笔记——第七章 视图技术Thymeleaf
  • 从比特保存和信息保存看数字资源长期保存
  • 兰伯特光照模型(Lambert Lighting)和半兰伯特光照模型(Half-Lanbert)
  • Python 进阶指南(编程轻松进阶):二、环境配置和命令行
  • 求职半年,三月成功拿到阿里offer,分享一波面经...
  • 餐饮店的运营需要考虑哪些方面
  • Multi-modal Alignment using Representation Codebook
  • 关于vector的emplace_back和push_back的区别
  • Vue——表单输入绑定
  • MySQL性能优化(二)索引
  • < 每日闲谈:你真的了解 “ ChatGPT ” 嘛 ? >
  • 改善Instagram客户服务的6个技巧
  • 8年经验之谈:4步解决测试与开发人员有争议的bug问题...
  • Linux日常小技巧shell脚本
  • 技术创业者必读:从验证想法到技术产品商业化的全方位解析
  • Docker Registry 本地镜像发布到私有库
  • Pytorch构建ResNet-50V2
  • 【01】PointNet论文解析
  • nuxt.js 在IE浏览器||其他浏览不识别document/window 情况处理
  • JavaEE简单示例——基于注解的SSM整合
  • EFBG-06-250双比例阀放大器
  • 初级算法-栈与队列