笔记整理自 coderwhy 『TypeScript 高阶数据结构与算法』课程

双向链表:拥有两个指针方向的链表

DoublyNode 结构:

  • prev:指向上一个节点
  • value:节点值
  • next:指向下一个节点

DoublyLinkedList 属性:

  • head:头节点
  • tail:尾节点
  • length:链表长度

实现方法:

  1. append:尾部插入节点
  2. prepend:头部插入节点
  3. traverse:正向遍历
  4. postTraverse:反向遍历
  5. getNodeByPosition:根据索引获取节点
  6. insert:根据索引插入节点
  7. removeAt:根据索引删除节点
  8. 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 实现】的更多相关文章

  1. JS数据结构与算法--双向链表

    双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示: 双向链表结构代码如下: class Node { constructor(element) { this.element ...

  2. 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述

    定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...

  3. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

  4. 《数据结构与算法JavaScript描述》

    <数据结构与算法JavaScript描述> 基本信息 作者: (美)Michael McMillan 译者: 王群锋 杜欢 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9 ...

  5. 面试常考的常用数据结构与算法(zz)

    数据结构与算法,这个部分的内容其实是十分的庞大,要想都覆盖到不太容易.在校学习阶段我们可能需要对每种结构,每种算法都学习,但是找工作笔试或者面试的时候,要在很短的时间内考察一个人这方面的能力,把每种结 ...

  6. Android版数据结构与算法(一):基础简介

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 一.前言 项目进入收尾阶段,忙忙碌碌将近一个多月吧,还好,不算太难,就是麻烦点. 数据结构与算法这个系列早就想写了,一是梳理总结,顺便逼迫自己把一 ...

  7. Python3-Cookbook总结 - 第一章:数据结构和算法

    第一章:数据结构和算法 Python 提供了大量的内置数据结构,包括列表,集合以及字典.大多数情况下使用这些数据结构是很简单的. 但是,我们也会经常碰到到诸如查询,排序和过滤等等这些普遍存在的问题. ...

  8. 数据结构与算法JS实现

      行解算法题,没错,就是这么方便. 当然也可以使用 Node.js 环境来执行,具体参考Node.js官方文档即可. 二 对象和面向对象编程 js中5种数据类型,并没有定义更多的数据类型,但是运用j ...

  9. Linux内核中常用的数据结构和算法(转)

    知乎链接:https://zhuanlan.zhihu.com/p/58087261 Linux内核代码中广泛使用了数据结构和算法,其中最常用的两个是链表和红黑树. 链表 Linux内核代码大量使用了 ...

  10. Python 数据结构和算法

    阅读目录 什么是算法 算法效率衡量 算法分析 常见时间复杂度 Python内置类型性能分析 数据结构 顺序表 链表 栈 队列 双端队列 排序与搜索 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归 ...

随机推荐

  1. 【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI

    问题描述 在Azure Spring Cloud中,通过ActiveDirectoryMSI方式来连接到SQL Service,需要如何配置呢? 问题分析 在SQL Service中启用Active ...

  2. 【Azure 微服务】记一次错误的更新Service Fabric 证书而引发的集群崩溃而只能重建

    问题描述 错误的操作步骤: 1)更新Service Fabric 的证书,制定了次要证书(Secondary),但是只修改了Service Fabric Cluster证书,而没有指定VMSS(虚拟机 ...

  3. RAPTOR 一种基于树的RAG方法,RAG的准确率提高 20%

    一种理解整个文档上下文的新颖的 RAG 方法 RAG 是当前使用LLM的标准方法,大多数现有方法仅从检索语料库中检索短的连续块,限制了对整个文档上下文的整体理解. 最近,一种名为 RAPTOR (Re ...

  4. 使用Mockito与Squaretest进行单元测试.

    项目开发过程中,不少公司都要求写单元测试的代码,可以提高代码的质量,并且可以减少出现BUG的概率. 对于中小型公司来说,对单元测试不做硬性要求,不写最好.因为还是需要一定的工作量,在保证代码质量和性能 ...

  5. [VueJsDev] 日志 - nginxConfig 配置文件备份

    [VueJsDev] 目录列表 https://www.cnblogs.com/pengchenggang/p/17037320.html nginxConfig 配置文件备份 ::: details ...

  6. 金蝶中间件 前后台连不上 报跨域 前台解决方案: --user-data-dir="c:\ChromeDebug" --test-type --disable-web-security

    chrome 浏览器的快捷方式后面加参数 --user-data-dir="c:\ChromeDebug" --test-type --disable-web-security

  7. 安装YCM

    安装Vundle git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim Vundle也是vim的 ...

  8. mysql迁移到pqsql笔记

    在将MySQL迁移到PostgreSQL的过程中,遇到了一些问题,下面是一些简单的解决方案. 使用命令,初始化数据库,并设置postgres的密码 bin\initdb -E UTF-8 -A md5 ...

  9. gcc生成静态链接库与动态链接库步骤,并链接生成可执行文件的简单示例

    编写 mylib.h void test(); 编写 mylib.c #include<stdio.h> void test(){ printf("hello world&quo ...

  10. CAST电子部单片机方向授课——串口通信 预习文档

    CAST电子部单片机方向授课--串口通信 预习文档 课前小准备 安装串口调试助手 第一步:进入Microsoft Store 第二步:在Microsoft Store中搜索 "串口调试助手& ...