实现双向链表
来源:dev.to
2024-11-18 12:50:32
0浏览
收藏
大家好,我们又见面了啊~本文《实现双向链表》的内容中将会涉及到等等。如果你正在学习文章相关知识,欢迎关注我,以后会给大家带来更多文章相关文章,希望我们能一起进步!下面就开始本文的正式内容~
假设理解 big o 表示法。 javascript 中有示例。资料参考 gayle laakmann mcdowell 的《cracking the coding interview》
理解双向链表
双向链表与单链表非常相似,除了节点的结构和添加/删除节点的方式不同。
节点结构双向链表中的节点包含
prev指针、next指针和value。 prev 指针指向前一个节点,next 指针指向下一个节点。本质上,这个列表在每个节点都是双向的。
添加节点在特定索引处插入新节点(newnode):
- 将当前位于插入索引处的节点存储在临时变量 nextnode 中。
- 更新前一个节点和新节点的连接:
- 将前一个节点的next指针设置为newnode。
- 设置newnode的prev指针指向前一个节点。
- 将新节点连接到下一个节点:
- 将newnode的next指针设置为nextnode。
- 将 nextnode 的 prev 指针设置为 newnode。
要删除特定索引处的节点:
- 更新前一个节点的next指针:
-
- 设置为指向被移除后的节点。
更新下一个节点的prev指针:
-
- 将其设置为指向被删除节点之前的节点。
时间复杂度分析
- 在双向链表中添加/删除 →
o(n)
- 在双向链表的头部或尾部添加/删除 →
o(1)
经典面向对象编程
class listnode {
constructor(value, prev = null, next = null) {
this.value = value;
this.prev = prev;
this.next = next;
}
}
class doublylinkedlist {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
// add a node to the head of the list
addhead(value) {
const newnode = new listnode(value, null, this.head);
if (this.head) {
this.head.prev = newnode;
} else {
this.tail = newnode; // if list was empty, new node is also the tail
}
this.head = newnode;
this.size++;
}
// add a node to the tail of the list
addtail(value) {
const newnode = new listnode(value, this.tail, null);
if (this.tail) {
this.tail.next = newnode;
} else {
this.head = newnode; // if list was empty, new node is also the head
}
this.tail = newnode;
this.size++;
}
// remove a node from the head of the list
removehead() {
if (!this.head) return null; // list is empty
const removedvalue = this.head.value;
this.head = this.head.next;
if (this.head) {
this.head.prev = null;
} else {
this.tail = null; // list became empty
}
this.size--;
return removedvalue;
}
// remove a node from the tail of the list
removetail() {
if (!this.tail) return null; // list is empty
const removedvalue = this.tail.value;
this.tail = this.tail.prev;
if (this.tail) {
this.tail.next = null;
} else {
this.head = null; // list became empty
}
this.size--;
return removedvalue;
}
// remove a node at a specific index
removeat(index) {
if (index < 0 || index >= this.size) return null;
let current;
if (index < this.size / 2) {
current = this.head;
for (let i = 0; i < index; i++) {
current = current.next;
}
} else {
current = this.tail;
for (let i = this.size - 1; i > index; i--) {
current = current.prev;
}
}
if (current.prev) current.prev.next = current.next;
if (current.next) current.next.prev = current.prev;
if (index === 0) this.head = current.next;
if (index === this.size - 1) this.tail = current.prev;
this.size--;
return current.value;
}
// get the size of the list
getsize() {
return this.size;
}
// get the values in the list
getvalues() {
const values = [];
let current = this.head;
while (current) {
values.push(current.value);
current = current.next;
}
return values;
}
}
函数式面向对象编程
function ListNode(value, prev = null, next = null) {
this.value = value;
this.prev = prev;
this.next = next;
}
function DoublyLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
// Add a node to the head of the list
DoublyLinkedList.prototype.addHead = function(value) {
const newNode = new ListNode(value, null, this.head);
if (this.head) {
this.head.prev = newNode;
} else {
this.tail = newNode;
}
this.head = newNode;
this.size++;
};
// Add a node to the tail of the list
DoublyLinkedList.prototype.addTail = function(value) {
const newNode = new ListNode(value, this.tail, null);
if (this.tail) {
this.tail.next = newNode;
} else {
this.head = newNode;
}
this.tail = newNode;
this.size++;
};
// Remove a node from the head of the list
DoublyLinkedList.prototype.removeHead = function() {
if (!this.head) return null;
const removedValue = this.head.value;
this.head = this.head.next;
if (this.head) {
this.head.prev = null;
} else {
this.tail = null;
}
this.size--;
return removedValue;
};
// Remove a node from the tail of the list
DoublyLinkedList.prototype.removeTail = function() {
if (!this.tail) return null;
const removedValue = this.tail.value;
this.tail = this.tail.prev;
if (this.tail) {
this.tail.next = null;
} else {
this.head = null;
}
this.size--;
return removedValue;
};
// Remove a node at a specific index
DoublyLinkedList.prototype.removeAt = function(index) {
if (index < 0 || index >= this.size) return null;
let current;
if (index < this.size / 2) {
current = this.head;
for (let i = 0; i < index; i++) {
current = current.next;
}
} else {
current = this.tail;
for (let i = this.size - 1; i > index; i--) {
current = current.prev;
}
}
if (current.prev) current.prev.next = current.next;
if (current.next) current.next.prev = current.prev;
if (index === 0) this.head = current.next;
if (index === this.size - 1) this.tail = current.prev;
this.size--;
return current.value;
};
// Get the size of the list
DoublyLinkedList.prototype.getSize = function() {
return this.size;
};
// Get the values in the list
DoublyLinkedList.prototype.getValues = function() {
const values = [];
let current = this.head;
while (current) {
values.push(current.value);
current = current.next;
}
return values;
};
今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
版本声明
本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除

- 上一篇
- Go 语言为什么要使用晚绑定?

- 下一篇
- 为什么我的函数定义中出现波浪线?
查看更多
最新文章
-
- 文章 · 前端 | 2分钟前 |
- CSS:list-style-type属性disc圆点和circle空心圆点有什么区别?
- 290浏览 收藏
-
- 文章 · 前端 | 4分钟前 |
- JS手把手教学:用ServiceWorker打造网站离线缓存
- 498浏览 收藏
-
- 文章 · 前端 | 10分钟前 | 布局设计 瀑布流
- html怎么实现瀑布流?超易上手的瀑布流布局教学
- 290浏览 收藏
-
- 文章 · 前端 | 12分钟前 |
- select+option超简单打造HTML下拉菜单,手把手教你快速实现dropdown菜单
- 220浏览 收藏
-
- 文章 · 前端 | 18分钟前 |
- Vue3CompositionAPI还是OptionsAPI?选它真的不亏!
- 409浏览 收藏
-
- 文章 · 前端 | 18分钟前 |
- html中td标签的作用?手把手教你快速掌握td标签三大功能
- 322浏览 收藏
-
- 文章 · 前端 | 28分钟前 |
- JS快速获取用户真实IP,这个方法超简单!
- 298浏览 收藏
-
- 文章 · 前端 | 29分钟前 |
- JS如何实现适配器模式?超详细案例教学
- 141浏览 收藏
-
- 文章 · 前端 | 32分钟前 | html
- HTMLprogress标签是啥?progress标签如何打造酷炫动态效果
- 412浏览 收藏
-
- 文章 · 前端 | 36分钟前 | CSS 呼吸灯
- 手把手教你用CSS实现呼吸灯效果,超简单呼吸动画教程!
- 375浏览 收藏
-
- 文章 · 前端 | 40分钟前 | 屏幕录制 网页录屏
- JS实现网页录屏的4种技术分享,轻松搞定屏幕录制
- 222浏览 收藏
-
- 文章 · 前端 | 42分钟前 | html time标签
- time标签怎么用?彻底搞懂它的语义化作用
- 220浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 96次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 100次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 106次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 101次使用
-
- Suno苏诺中文版
- 探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
- 99次使用
查看更多
相关文章
-
- 优化用户界面体验的秘密武器:CSS开发项目经验大揭秘
- 2023-11-03 501浏览
-
- 使用微信小程序实现图片轮播特效
- 2023-11-21 501浏览
-
- 解析sessionStorage的存储能力与限制
- 2024-01-11 501浏览
-
- 探索冒泡活动对于团队合作的推动力
- 2024-01-13 501浏览
-
- UI设计中为何选择绝对定位的智慧之道
- 2024-02-03 501浏览