双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示:

双向链表结构代码如下:

class Node {
constructor(element) {
this.element = element;
this.prev = null;
this.next = null;
}
} class DoubledLinskLIst {
constructor() {
this.length = 0;
this.head = null;
this.tail = null;
} append(element) {
let node = new Node(element);
if (this.getHead() === null) { //空链表的情况
this.head = node;
this.tail = node; //如果是空链表,插入一个元素,它的head和tail都指向node
}
else {
let current = this.getTail();
current.next = node;
node.prev = current;
this.tail = node;
}
this.length++;
} insert(position, element) { if (position >= 0 && position <= this.size()) {
let current = this.getHead(),
previous,
index = 0,
node = new Node(element);
if (position === 0) {
if (!this.getHead()) { //空链表
this.head = node;
this.tail = node;
}
else {
this.head = node;
node.next = current;
current.prev = node;
} }
else if (position === this.size()) {
current = this.getTail();
this.tail = node;
node.prev = current;
current.next = node;
}
else { while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = node;
node.prev = previous;
node.next = current;
current.prev = node; }
this.length++;
return true;
}
else {
return false;
}
} removeAt(position) {
if (position >= 0 && position < this.size()) {
let current = this.getHead(), index = 0, previous;
if (position === 0) {
this.head = current.next;
if (this.size() === 1) { //链表只有一个数据
this.tail = null;
}
else {
current.next.prev = null;
} }
else if (position === this.size() - 1) {
current = this.getTail();
this.tail = current.prev;
current.prev.next = null;
}
else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous; }
this.length--;
return current.element;
}
else {
return null;
}
} indexOf(element) {
let current = this.getHead(), index = 0;
while (current) {
if (current.element === element) {
return index;
}
index++;
current = current.next;
}
return -1;
} remove(element) {
let position = this.indexOf(element);
return this.removeAt(position);
} getHead() {
return this.head;
} getTail() {
return this.tail;
} size() {
return this.length;
} isEmpty() {
return this.length === 0;
} toString() { let current = this.getHead(),
string = ''; while (current) {
string += current.element + (current.next ? ', ' : '');
current = current.next;
}
return string; } print() {
console.log(this.toString());
}
}

参考:《JavaScript数据结构与算法--第二版》

JS数据结构与算法--双向链表的更多相关文章

  1. js数据结构与算法--双向链表的实现

    双向链表也叫双链表,是链表的一种,它的每个数据节点中都有两个指针,分别指向直接后继和直接前驱.所以,双向链表中的任意一个节点开始,都可以很方便的访问它的前驱节点和后继节点. 双向链表的实现 linke ...

  2. JS数据结构与算法——栈

    JS数据结构与算法--栈 1.栈结构概念 栈(Stack)是一种先进后出(LIFO Last in First out)的线性表,先进栈的将会比后进栈的先出栈. 栈的限制是仅允许在一端进行插入和删除运 ...

  3. JS数据结构与算法-概述

    JS数据结构与算法概述 数据结构: 计算机存储, 组织数据的方式, 就像锅碗瓢盆 算法: 一系列解决问题的清晰指令, 就像食谱 两者关系: 程序 = 数据结构 + 算法 邂逅数据结构与算法 什么是数据 ...

  4. JS数据结构及算法(二) 队列

    队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...

  5. JS数据结构及算法(一) 堆栈

    最近在看<学习JavaScript数据结构与算法>这本书,感觉自己又涨知识了 哈哈... 现在将自己看的做个总结,也是巩固理解. 栈:先进后出,新添加和待删除的元素都保存在栈顶.可以用数组 ...

  6. js数据结构与算法--单链表的实现与应用思考

    链表是动态的数据结构,它的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 现实中,有一些链表的例子. 第一个就是寻宝的游戏.你有一条线索,这条线索是指向寻找下一条线 ...

  7. JS数据结构与算法 - 剑指offer二叉树算法题汇总

    ❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...

  8. js数据结构与算法存储结构

    数据结构(程序设计=数据结构+算法) 数据结构就是关系,没错,就是数据元素相互之间存在的一种或多种特定关系的集合. 传统上,我们把数据结构分为逻辑结构和物理结构. 逻辑结构:是指数据对象中数据元素之间 ...

  9. JS数据结构与算法-队列结构

    队列结构 一.认识队列 受限的线性结构: 我们已经学习了一种受限的线性结构:栈结构. 并且已经知道这种受限的数据结构对于解决某些特定问题,会有特别的 效果. 下面,我们再来学习另外一个受限的数据结构: ...

随机推荐

  1. 前端面试题整理---JS基础

    为了督促自己学习,整理了一下前端的面试题 JavaScript: JavaScript 中如何监测一个变量是String类型? typeof(obj)==="string"; ty ...

  2. python 定位

    #字符串定位 使用str.find() 其结果为如下: #列表中元素的定位 使用list.index() 其结果如下:

  3. 一个小时学会 MySQL 数据库

    随着移动互联网的结束与人工智能的到来大数据变成越来越重要,下一个成功者应该是拥有海量数据的,数据与数据库你应该知道. 一.数据库概要 数据库(Database)是存储与管理数据的软件系统,就像一个存入 ...

  4. Codeforces Round #564 (Div. 2) B. Nauuo and Chess

    链接:https://codeforces.com/contest/1173/problem/B 题意: Nauuo is a girl who loves playing chess. One da ...

  5. Web自动化测试—PO设计模式(一)

    前言 很多的测试同学懂得使用selenium进行Web自动化测试, 但是不知道如何去写一个测试框架,或者说是一个容易维护的web自动化项目. 自己写一个最基本的web自动化测试框架需要会什么? 1. ...

  6. 优化MyEclipse编译速度慢的问题

    转载大神的 https://www.cnblogs.com/luxd/p/5999217.html

  7. IIS 在 Windows 上托管 .NET Core2.0

    使用 IIS 在 Windows 上托管 ASP.NET Core2.0 https://www.cnblogs.com/sundar/p/9195550.html 阅读目录 准备: 第一步:新建项目 ...

  8. NET Core 防止跨站请求

    ASP.NET Core 防止跨站请求伪造(XSRF/CSRF)攻击 什么是反伪造攻击? 跨站点请求伪造(也称为XSRF或CSRF,发音为see-surf)是对Web托管应用程序的攻击,因为恶意网站可 ...

  9. SpringBoot---Web开发---WebSocket

    [广播式] 1. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  10. 博弈论 && 题目

    终于我也开始学博弈了,说了几个月,现在才学.学多点套路,不深学.(~~) 参考刘汝佳蓝书p132 nim游戏: 假设是两维的取石子游戏,每次可以在任意一堆拿任意数量个(至少一根,因为这样游戏的状态集有 ...