登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
23
Star
192
Fork
47
yanleweb
/
interview-question
代码
Issues
1091
Pull Requests
0
Wiki
统计
流水线
服务
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
实现一个双向链表, 具备添加节点、删除节点、在特定位置插入节点、查找节点、遍历等功能
待办的
#I6N4XE
yanleweb
拥有者
创建于
2023-03-14 22:29
# 必须要掌握的知识 在 JavaScript 中实现双向链表需要掌握以下知识点: - 如何使用构造函数和类创建双向链表节点,以及如何在节点之间建立双向连接。 - 双向链表的常用操作,包括`添加节点、删除节点、在特定位置插入节点、查找节点`等。 - 双向链表的遍历和迭代,包括`正向遍历、反向遍历、循环遍历`等。 - 链表的常见问题,例如`链表是否为空、链表长度、查找节点`等。 - 对 JavaScript 垃圾回收机制的理解,确保双向链表的实现不会导致内存泄漏。 以上知识点是实现双向链表所必须掌握的内容,掌握这些知识点能够帮助我们有效地创建和操作双向链表。 ## 什么是双向链表 双向链表(Doubly linked list)是一种常见的数据结构,它是由一系列节点组成的,每个节点都包含一个指向前驱节点和后继节点的指针。相比单向链表,双向链表具有双向遍历的能力,即可以从任意一个节点开始,向前或向后遍历整个链表。 双向链表的每个节点通常包含两个指针,即 prev 指针和 next 指针。prev 指针指向当前节点的前驱节点,而 next 指针指向当前节点的后继节点。由于每个节点都包含两个指针,因此双向链表的节点通常比单向链表的节点更占用空间。 双向链表可以用于实现各种数据结构和算法,如LRU(Least Recently Used)缓存淘汰算法,双向队列(Deque)等。由于它具有双向遍历的能力,因此在某些场景下可以比单向链表更加高效和方便。 ## 实现一个双向链表 ```js class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; } // 在链表末尾添加节点 push(value) { const node = new Node(value); if (this.length === 0) { this.head = node; this.tail = node; } else { this.tail.next = node; node.prev = this.tail; this.tail = node; } this.length++; return this; } // 从链表末尾移除节点 pop() { if (this.length === 0) { return undefined; } const node = this.tail; if (this.length === 1) { this.head = null; this.tail = null; } else { this.tail = node.prev; this.tail.next = null; node.prev = null; } this.length--; return node.value; } // 在链表开头添加节点 unshift(value) { const node = new Node(value); if (this.length === 0) { this.head = node; this.tail = node; } else { this.head.prev = node; node.next = this.head; this.head = node; } this.length++; return this; } // 从链表开头移除节点 shift() { if (this.length === 0) { return undefined; } const node = this.head; if (this.length === 1) { this.head = null; this.tail = null; } else { this.head = node.next; this.head.prev = null; node.next = null; } this.length--; return node.value; } // 获取指定位置的节点 get(index) { if (index < 0 || index >= this.length) { return undefined; } let node = null; if (index < this.length / 2) { node = this.head; for (let i = 0; i < index; i++) { node = node.next; } } else { node = this.tail; for (let i = this.length - 1; i > index; i--) { node = node.prev; } } return node; } // 在指定位置插入节点 insert(index, value) { if (index < 0 || index > this.length) { return false; } if (index === 0) { return !!this.unshift(value); } if (index === this.length) { return !!this.push(value); } const node = new Node(value); const prevNode = this.get(index - 1); const nextNode = prevNode.next; prevNode.next = node; node.prev = prevNode; node.next = nextNode; nextNode.prev = node; this.length++; return true; } // 移除指定位置的节点 remove(index) { if (index < 0 || index >= this.length) { return undefined; } if (index === 0) { return this.shift(); } if (index === this.length - 1) { return this.pop(); } const nodeToRemove = this.get(index); const prevNode = nodeToRemove.prev; const nextNode = nodeToRemove.next; prevNode.next = nextNode; nextNode.prev = prevNode; nodeToRemove.next = null; nodeToRemove.prev = null; this.length--; return nodeToRemove.value; } // 反转链表 reverse() { let node = this.head; this.head = this.tail; this.tail = node; let prevNode = null; let nextNode = null; for (let i = 0; i < this.length; i++) { nextNode = node.next; node.next = prevNode; node.prev = nextNode; prevNode = node; node = nextNode; } return this; } // 通过 value 来查询 index findIndexByValue(value) { let currentNode = this.head; let index = 0; while (currentNode) { if (currentNode.value === value) { return index; } currentNode = currentNode.next; index++; } return -1; // 如果链表中没有找到该值,返回 -1 } // 正向遍历链表,并返回遍历结果 forwardTraversal() { const result = []; let current = this.head; while (current) { result.push(current.value); current = current.next; } return result; } // 反向遍历链表,并返回遍历结果 backwardTraversal() { const result = []; let current = this.tail; while (current) { result.push(current.value); current = current.prev; } return result; } // 循环遍历链表,并返回遍历结果 loopTraversal() { const result = []; let current = this.head; while (current) { result.push(current.value); current = current.next; if (current === this.head) { break; } } return result; } } ```
# 必须要掌握的知识 在 JavaScript 中实现双向链表需要掌握以下知识点: - 如何使用构造函数和类创建双向链表节点,以及如何在节点之间建立双向连接。 - 双向链表的常用操作,包括`添加节点、删除节点、在特定位置插入节点、查找节点`等。 - 双向链表的遍历和迭代,包括`正向遍历、反向遍历、循环遍历`等。 - 链表的常见问题,例如`链表是否为空、链表长度、查找节点`等。 - 对 JavaScript 垃圾回收机制的理解,确保双向链表的实现不会导致内存泄漏。 以上知识点是实现双向链表所必须掌握的内容,掌握这些知识点能够帮助我们有效地创建和操作双向链表。 ## 什么是双向链表 双向链表(Doubly linked list)是一种常见的数据结构,它是由一系列节点组成的,每个节点都包含一个指向前驱节点和后继节点的指针。相比单向链表,双向链表具有双向遍历的能力,即可以从任意一个节点开始,向前或向后遍历整个链表。 双向链表的每个节点通常包含两个指针,即 prev 指针和 next 指针。prev 指针指向当前节点的前驱节点,而 next 指针指向当前节点的后继节点。由于每个节点都包含两个指针,因此双向链表的节点通常比单向链表的节点更占用空间。 双向链表可以用于实现各种数据结构和算法,如LRU(Least Recently Used)缓存淘汰算法,双向队列(Deque)等。由于它具有双向遍历的能力,因此在某些场景下可以比单向链表更加高效和方便。 ## 实现一个双向链表 ```js class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; } // 在链表末尾添加节点 push(value) { const node = new Node(value); if (this.length === 0) { this.head = node; this.tail = node; } else { this.tail.next = node; node.prev = this.tail; this.tail = node; } this.length++; return this; } // 从链表末尾移除节点 pop() { if (this.length === 0) { return undefined; } const node = this.tail; if (this.length === 1) { this.head = null; this.tail = null; } else { this.tail = node.prev; this.tail.next = null; node.prev = null; } this.length--; return node.value; } // 在链表开头添加节点 unshift(value) { const node = new Node(value); if (this.length === 0) { this.head = node; this.tail = node; } else { this.head.prev = node; node.next = this.head; this.head = node; } this.length++; return this; } // 从链表开头移除节点 shift() { if (this.length === 0) { return undefined; } const node = this.head; if (this.length === 1) { this.head = null; this.tail = null; } else { this.head = node.next; this.head.prev = null; node.next = null; } this.length--; return node.value; } // 获取指定位置的节点 get(index) { if (index < 0 || index >= this.length) { return undefined; } let node = null; if (index < this.length / 2) { node = this.head; for (let i = 0; i < index; i++) { node = node.next; } } else { node = this.tail; for (let i = this.length - 1; i > index; i--) { node = node.prev; } } return node; } // 在指定位置插入节点 insert(index, value) { if (index < 0 || index > this.length) { return false; } if (index === 0) { return !!this.unshift(value); } if (index === this.length) { return !!this.push(value); } const node = new Node(value); const prevNode = this.get(index - 1); const nextNode = prevNode.next; prevNode.next = node; node.prev = prevNode; node.next = nextNode; nextNode.prev = node; this.length++; return true; } // 移除指定位置的节点 remove(index) { if (index < 0 || index >= this.length) { return undefined; } if (index === 0) { return this.shift(); } if (index === this.length - 1) { return this.pop(); } const nodeToRemove = this.get(index); const prevNode = nodeToRemove.prev; const nextNode = nodeToRemove.next; prevNode.next = nextNode; nextNode.prev = prevNode; nodeToRemove.next = null; nodeToRemove.prev = null; this.length--; return nodeToRemove.value; } // 反转链表 reverse() { let node = this.head; this.head = this.tail; this.tail = node; let prevNode = null; let nextNode = null; for (let i = 0; i < this.length; i++) { nextNode = node.next; node.next = prevNode; node.prev = nextNode; prevNode = node; node = nextNode; } return this; } // 通过 value 来查询 index findIndexByValue(value) { let currentNode = this.head; let index = 0; while (currentNode) { if (currentNode.value === value) { return index; } currentNode = currentNode.next; index++; } return -1; // 如果链表中没有找到该值,返回 -1 } // 正向遍历链表,并返回遍历结果 forwardTraversal() { const result = []; let current = this.head; while (current) { result.push(current.value); current = current.next; } return result; } // 反向遍历链表,并返回遍历结果 backwardTraversal() { const result = []; let current = this.tail; while (current) { result.push(current.value); current = current.prev; } return result; } // 循环遍历链表,并返回遍历结果 loopTraversal() { const result = []; let current = this.head; while (current) { result.push(current.value); current = current.next; if (current === this.head) { break; } } return result; } } ```
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
JavaScript
未设置
标签管理
里程碑
中
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (1)
标签 (64)
master
0.0.76
0.0.75
0.0.74
0.0.73
0.0.72
0.0.71
0.0.70
0.0.69
0.0.68
0.0.67
0.0.66
0.0.65
0.0.64
0.0.63
0.0.62
0.0.61
0.0.60
0.0.59
0.0.58
0.0.57
0.0.56
0.0.55
0.0.54
0.0.53
0.0.52
0.0.51
0.0.50
0.0.49
0.0.48
0.0.47
0.0.46
0.0.45
0.0.44
0.0.43
0.0.42
0.0.41
0.0.40
0.0.39
0.0.38
0.0.37
0.0.36
0.0.35
0.0.34
0.0.33
0.0.32
0.0.31
0.0.30
0.0.29
0.0.28
0.0.27
0.0.26
0.0.25
0.0.24
0.0.23
0.0.22
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.14
0.0.13
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
TypeScript
1
https://gitee.com/yanleweb/interview-question.git
git@gitee.com:yanleweb/interview-question.git
yanleweb
interview-question
interview-question
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册