笔记整理自 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 的应用 使用Service Bus 引起 org.springframework.beans.BeanInstantiationException 异常,无法启动

    问题描述 Spring Cloud 的应用原本正常运行,但是重新发布了一次应用后,发现使用 Service Bus 服务的应用都无法启动并报 BeanInstantiationException  异 ...

  2. php的php-fpm

    FastCgi与PHP-fpm到底是个什么样的关系 昨晚有一位某知名在线教育的大佬问了我一个问题,你知道php-fpm和cgi之间的关系吗?作为了一个5年的phper了,这个还不是很简单的问题,然后我 ...

  3. mysql中innodb创建表的一些限制

    1. 背景 在新创建mysql数据表的时候.不太确定表能创建多少个字段,多少个索引.索引多少有限制么?mysql的数据是怎么存储的存在在哪里. 2.基本个数限制 在MySQL5.6.9以后的版本,一个 ...

  4. git 常见命令和资源

    git练习 常用git清单 强制切换分支所指位置 git branch -f main c3强制分支main指向c3 git branch -f main HEAD~3强制分支main指向head的父 ...

  5. 【主流技术】日常工作中关于 JSON 转换的经验大全(Java)

    目录 前言 一.JSON 回顾 1.1结构形式 二.其它类型 -> JSON相关 2.1 JavaBean 转 JsonObject 2.2 JavaBean 转 Json 字符串 2.3 Li ...

  6. 玩转Vue3之shallowRef和shallowReactive

    前言 Vue3 作为一款现代的 JavaScript 框架,引入了许多新的特性和改进,其中包括 shallowRef 和 shallowReactive.这两个功能在Vue 3中提供了更加灵活和高效的 ...

  7. stm32 文件系统数据读写源码解析

    一 概念 fatfs文件系统在文件读写中不可或却.熟悉和深入理解是一个不可或缺的前提. 这里面需要先明确几个概念:文件open的属性,这个非常重要.可以并列使用. 二  源码解析 A  写入数据: i ...

  8. stm32 使用多串口通信调试总结

    前记:   stm32使用多个串口通信,这个项目遇到了不少问题,值得反思和深入总结一下.   提纲:  这次的问题,主要有几个部分组成: A 多串口的DMA配置,这个需要注意,尽量不要使用同一个DMA ...

  9. 自己想到的几道Java面试题

    1.在抽象类中能否写main方法,为什么? 2.在接口中能否写main方法,为什么? 3.Java能否使用静态局部变量,为什么? 4.Java类变量,实例变量,局部变量在多线程环境下是否线程安全,为什 ...

  10. Java问题汇总,持续更新到GitHub

    目录介绍 00.Java问题汇总 01.具体问题 好消息 博客笔记大汇总[16年3月到至今],包括Java基础及深入知识点,Android技术博客,Python学习笔记等等,还包括平时开发中遇到的bu ...