双向链表的基本实现【数据结构与算法—TypeScript 实现】
笔记整理自 coderwhy 『TypeScript 高阶数据结构与算法』课程
双向链表:拥有两个指针方向的链表
DoublyNode 结构:
- prev:指向上一个节点
- value:节点值
- next:指向下一个节点
DoublyLinkedList 属性:
- head:头节点
- tail:尾节点
- length:链表长度
实现方法:
- append:尾部插入节点
- prepend:头部插入节点
- traverse:正向遍历
- postTraverse:反向遍历
- getNodeByPosition:根据索引获取节点
- insert:根据索引插入节点
- removeAt:根据索引删除节点
- Size:获取链表长度
具体代码实现:
class DoublyNode<T> {
value: T;
next: DoublyNode<T> | null = null;
prev: DoublyNode<T> | null = null;
constructor(value: T) {
this.value = value;
}
}
class DoublyLinkedList<T> {
head: DoublyNode<T> | null = null;
tail: DoublyNode<T> | null = null;
length: number = 0;
constructor() {}
// 获取链表长度
get Size(): number {
return this.length;
}
// 尾部插入节点
append(value: T): void {
const newNode = new DoublyNode(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail!.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
this.length++;
}
// 头部插入节点
prepend(value: T): void {
// 创建一个新节点
const newNode = new DoublyNode(value);
// 链表无数据,即 head 为空
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
// 处理以前的 head
newNode.next = this.head;
this.head.prev = newNode;
// 更新 head
this.head = newNode;
}
// 链表数据增加
this.length++;
}
// 正向遍历
traverse() {
let current = this.head;
let _arr: (T | null)[] = [];
while (current) {
_arr.push(current.value);
current = current.next;
}
console.log(_arr.join(" -> "));
}
// 反向遍历
postTraverse() {
let current = this.tail;
let _arr: T[] = [];
while (current) {
_arr.push(current.value);
current = current.prev;
}
console.log(_arr.join(" -> "));
}
// 根据索引获取当前节点
getNodeByPosition(position: number): DoublyNode<T> | null {
let index = 0;
let current = this.head;
while (index++ < position && current) {
current = current.next;
}
return current;
}
// 根据索引插入元素
insert(value: T, position: number): boolean {
// 越界判断
if (position < 0 || position > this.length) return false;
// 头部插入
if (position === 0) {
this.prepend(value);
} else if (position === this.length) {
// 尾部插入
this.append(value);
} else {
// 根据 position 插入
const newNode = new DoublyNode(value);
let current = this.getNodeByPosition(position);
current!.prev!.next = newNode;
newNode.prev = current!.prev;
newNode.next = current;
current!.prev = newNode;
this.length++;
}
return true;
}
// 根据索引删除元素
removeAt(position: number): T | null {
// 1. 越界判断
if (position < 0 || position >= this.length) return null;
// 2. 合规情况分析
let current = this.head;
// 2.1 删除头部
if (position === 0) {
// 只有一个节点
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
// 多个节点
this.head = this.head!.next;
this.head!.prev = null;
}
} else if (position === this.length - 1) {
// 2.2 删除尾部
current = this.tail;
this.tail = this.tail!.prev;
this.tail!.next = null;
} else {
// 2.3 其余情况
const node = this.getNodeByPosition(position);
current = node;
node!.prev!.next = node!.next;
node!.next!.prev = node!.prev;
}
this.length--;
return current ? current.value : null;
}
}
const doublyLinkedList = new DoublyLinkedList<string>();
doublyLinkedList.append("aaa");
doublyLinkedList.append("bbb");
doublyLinkedList.append("ccc");
doublyLinkedList.insert("ddd", 0);
doublyLinkedList.removeAt(0);
doublyLinkedList.traverse();
双向链表的基本实现【数据结构与算法—TypeScript 实现】的更多相关文章
- JS数据结构与算法--双向链表
双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示: 双向链表结构代码如下: class Node { constructor(element) { this.element ...
- 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述
定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...
- 数据结构与算法 Big O 备忘录与现实
不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新. 算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...
- 《数据结构与算法JavaScript描述》
<数据结构与算法JavaScript描述> 基本信息 作者: (美)Michael McMillan 译者: 王群锋 杜欢 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9 ...
- 面试常考的常用数据结构与算法(zz)
数据结构与算法,这个部分的内容其实是十分的庞大,要想都覆盖到不太容易.在校学习阶段我们可能需要对每种结构,每种算法都学习,但是找工作笔试或者面试的时候,要在很短的时间内考察一个人这方面的能力,把每种结 ...
- Android版数据结构与算法(一):基础简介
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 一.前言 项目进入收尾阶段,忙忙碌碌将近一个多月吧,还好,不算太难,就是麻烦点. 数据结构与算法这个系列早就想写了,一是梳理总结,顺便逼迫自己把一 ...
- Python3-Cookbook总结 - 第一章:数据结构和算法
第一章:数据结构和算法 Python 提供了大量的内置数据结构,包括列表,集合以及字典.大多数情况下使用这些数据结构是很简单的. 但是,我们也会经常碰到到诸如查询,排序和过滤等等这些普遍存在的问题. ...
- 数据结构与算法JS实现
行解算法题,没错,就是这么方便. 当然也可以使用 Node.js 环境来执行,具体参考Node.js官方文档即可. 二 对象和面向对象编程 js中5种数据类型,并没有定义更多的数据类型,但是运用j ...
- Linux内核中常用的数据结构和算法(转)
知乎链接:https://zhuanlan.zhihu.com/p/58087261 Linux内核代码中广泛使用了数据结构和算法,其中最常用的两个是链表和红黑树. 链表 Linux内核代码大量使用了 ...
- Python 数据结构和算法
阅读目录 什么是算法 算法效率衡量 算法分析 常见时间复杂度 Python内置类型性能分析 数据结构 顺序表 链表 栈 队列 双端队列 排序与搜索 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归 ...
随机推荐
- NET项目&DLL反编译&MSSQL监控&VS搜索&注入&上传
知识点 1.NET普通源码&编译源码 2.DLL反编译&后缀文件&指向 3.代码审计-SQL注入&文件上传 ASPX文件 -> CS ASPX.CS DLL反编译 ...
- node.js在win7下安装,并测试是否安装成功
1.node.js去官网下载,下载完,像平时安装软件一样 2.把下面的测试文件,放到安装目录下,本文是放到:D:\Program Files\nodejs下 var http = require(&q ...
- 英语自定义标签 <i:juzi><i:zhuyu>John Smith</i:zhuyu></i:juzi> 主语谓语宾语
效果 John Smith died in World War Two. John Smith killed three enemy soldiers. <style> i\:juzi { ...
- vue遇到拖拽动态生成组件怎么办?[转]
知识点 主要是关注 动态生成 vue组件,这里是Vue2.0的demo Vue.Draggable 拖拽库 Vue.extend() 挂载 com.$mount() 生成组件 this.$refs.c ...
- docker安装kafka和zookeeper
参考,欢迎点击原文:https://www.cnblogs.com/360minitao/p/14665845.html(主要) https://blog.csdn.net/qq_22041375/a ...
- linux shell 字体颜色设置
使用 echo -e "\033[0;32;40m" 可以将字体设置成绿色. 这里必须使用echo 的选项 "-e",因为后面需要用到转义序列. 转义序列就是一 ...
- 编译OpenWRT-for-MT7620A(带8021x验证)
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明 本文作为本人csdn blog的主站的备份.(Bl ...
- Linux常用指令2
1.系统常用命令 1)在文件中查找内容 grep >grep hello passwd //在passwd文件中搜索hello内容,会把hello所在行的内容打印到终端显示 2)查看系统中活跃 ...
- 记录-因为写不出拖拽移动效果,我恶补了一下Dom中的各种距离
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 背景 最近在项目中要实现一个拖拽头像的移动效果,一直对JS Dom拖拽这一块不太熟悉,甚至在网上找一个示例,都看得云里雾里的,发现遇到最大 ...
- 使用graphviz图形化展示路径(决策树用到此库)
问题:当出现错误 graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot'), make sur ...