笔记整理自 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 应用服务】调用Azure Function经常提示超时的分析

    问题描述 Azure Data Factory 通过 Pipeline 调用Azure Function Http Trigger时遇到返回错误" 500 - The request tim ...

  2. 答对这 9 题你就超越了 83.3% 的图数据库 NebulaGraph 用户

    熟悉 NebulaGraph 社区的小伙伴可能都知道一个技能认证叫做:NGCP,全称 NebulaGraph Certified Professional.用户在考试认证期间在 1 个小时内回答 10 ...

  3. PostgreSQL、KingBase 数据库 ORDER BY LIMIT 查询缓慢案例

    好久没写博客了,最近从人大金仓离职了,新公司入职了蚂蚁集团,正在全力学习 OcenaBase 数据库的体系结构中. 以后分享的案例知识基本上都是以 OcenaBase 分布式数据库为主了,呦西. 昨天 ...

  4. Java 开发人员调度软件项目 (java基础编程总结项目)+javaBean+测试代码+数组知识+数据结构+继承+多态+封装+自定义异常,异常处理+构造器知识+重载+重写+接口+实现接口+关键字使用(static +equalsIgnoreCase+fianl+instanceof判断类型)+向下转型与向上转型

    /** * * @Description Java 开发人员调度软件项目 (java基础编程总结项目) * +javaBean+测试代码+数组知识+数据结构+继承+多态+封装+自定义异常,异常处理 * ...

  5. Django进阶之路由层和视图层

    Django的路由系统 [1]什么是URL配置(URLconf) URL调度器 | Django 文档 | Django (djangoproject.com) URL配置(URLconf)就像Dja ...

  6. vscode git冲突 1. git stash 2. 更新代码 3. git stash pop 4.提交代码

    vscode git冲突 1. git stash 2. 更新代码 3. git stash pop 4.提交代码

  7. 关于vscode的复制粘贴的问题

    有的是因为安装了vim的插件,卸掉即可.或者直接在快捷键设置里面直接改变复制粘贴的快捷键!

  8. String.equals(Object anObject)方法

    首先注意,equals()方法接受的是Object类型的对象,并不一定是String类型. public boolean equals(Object anObject) { //两个对象地址是否一样, ...

  9. java unsigned int,int,long

    java 中没有unsigned int,处理这个要采用long. int x = (1<<31) 与int x= -(1<<31)答案是相同的 0xffff ffff 与0x ...

  10. 多线程系列(二十一) -ForkJoin使用详解

    一.摘要 从 JDK 1.7 开始,引入了一种新的 Fork/Join 线程池框架,它可以把一个大任务拆成多个小任务并行执行,最后汇总执行结果. 比如当前要计算一个数组的和,最简单的办法就是用一个循环 ...