原文:

  在 Javascript 中学习数据结构与算法。

概念:

  链表存储有序的元素集合,但不同于数组,链表中的元素在内存中并不是连续放置的。每个 元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成。下图展示了链表的结构:

  相对于传统的数组,链表的一个好处在于,添加或移除元素的时候不需要移动其他元素。然而,链表需要使用指针,因此实现链表时需要额外注意。 数组的另一个细节是可以直接访问任何位置的任何元素,而要想访问链表中间的一个元素,需要从起点(表头)开始迭代列表直到找到所需的元素。

普通链表:

// 链表节点
class Node {
constructor(element) {
this.element = element;
this.next = null;
}
} // 链表
class LinkedList {
constructor() {
this.head = null;
this.length = 0; // length 同数组 length 与下标关系
} // 追加元素
append(element) {
let node = new Node(element);
let current = null; // 指针? if (this.head === null) {
this.head = node;
} else {
current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this.length++;
} // 任意位置插入元素
insert (position, element) {
if (position >= 0 && position <= this.length) {
let node = new Node(element);
let current = this.head;
let previous = null;
let index = 0;
if (position === 0) {
this.head = node;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
this.length++;
return true
}
return false
} // 移除指定位置元素
removeAt(position) {
if (position > -1 && position < length) {
let current = this.head;
let previous = null;
let index = 0;
if (position === 0) {
this.head = current.next;
} else {
while(index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
this.length--;
return current.element;
}
return null
} // 寻找元素下标
findIndex(element) {
let current = this.head;
let index = -1;
while (current) {
if (element === current.element) {
return index + 1;
}
index++;
current = current.next;
} return -1;
} // 删除指定文档
remove(element) {
let index = this.findIndex(element);
return removeAt(index);
} isEmpty() {
return !this.length;
} size() {
return this.length;
} // 输出字符串
toString() {
let current = this.head;
let string = '';
while (current) {
string += ` ${current.element}`;
current = current.next;
}
return string;
}
} var ll = new LinkedList();
console.log(ll);
ll.append(2);
ll.append(6);
ll.append(24);
ll.append(152); ll.insert(3, 18);
console.log(ll);
console.log(ll.findIndex(24));

双向链表:

class Node {
constructor(element) {
this.element = element;
this.prev = null;
this.next = null;
}
} // 双向链表
class DoubleLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
} // 任意位置插入元素
insert(position, element) {
if (position >= 0 && position <= ehis.length) {
let node = new Node(element);
let current = this.head;
let previous = null;
this.index = 0;
// 首位
if (position === 0) {
if (!head) {
this.head = node;
this.tail = node;
} else {
node.next = current;
this.head = node;
current.prev = node;
}
} else if (position === this.length) { // 末尾
current = this.tail;
current.next = node;
node.prev = current;
this.tail = node;
} else { // 中间
while(index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
current.prev = node;
node.prev = previous;
}
this.length++;
return true;
}
return false;
}
// 移除指定位置元素
removeAt(position) {
if (position > -1 && position < this.length) {
let current = this.head;
let previous = null;
let index = 0; // 首位
if (position === 0) {
this.head = this.head.next
this.head.prev = null
if (this.length === 1) {
this.tail = null
}
} else if (position === this.length - 1) { // 末位
this.tail = this.tail.prev
this.tail.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;
}
} // 其他方法
}

循环链表:

  具体代码实现就不写了,下图是示意图

js 实现数据结构 -- 链表的更多相关文章

  1. (js描述的)数据结构[链表](4)

    (js描述的)数据结构 [链表](4) 一.基本结构 二.想比于数组,链表的一些优点 1.内存空间不是必须连续的,可以充分利用计算机的内存,事项灵活的内存动态管理. 2.链表不必再创建时就确定大小,并 ...

  2. JS实现单链表、单循环链表

    链表 链表是一种物理存储单元上非线性.非连续性的数据结构(它在数据逻辑上是线性的),它的每个节点由两个域组成:数据域和指针域.数据域中存储实际数据,指针域则存储着指针信息,指向链表中的下一个元素或者上 ...

  3. 再谈js对象数据结构底层实现原理-object array map set

    如果有java基础的同学,可以回顾下<再谈Java数据结构—分析底层实现与应用注意事项>:java把内存分两种:一种是栈内存,另一种是堆内存.基本类型(即int,short,long,by ...

  4. Python—数据结构——链表

    数据结构——链表 一.简介 链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性存储结构.由一系列节点组成的元素集合.每个节点包含两部分,数据域item和指向下一个节 ...

  5. 数据结构和算法(Golang实现)(12)常见数据结构-链表

    链表 讲数据结构就离不开讲链表.因为数据结构是用来组织数据的,如何将一个数据关联到另外一个数据呢?链表可以将数据和数据之间关联起来,从一个数据指向另外一个数据. 一.链表 定义: 链表由一个个数据节点 ...

  6. Redis数据结构—链表与字典的结构

    目录 Redis数据结构-链表与字典的结构 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 Redis字典的使用 Re ...

  7. Redis数据结构—链表与字典

    目录 Redis数据结构-链表与字典 链表 Redis链表节点的结构 Redis链表的表示 Redis链表用在哪 字典 Redis字典结构总览 Redis字典结构分解 哈希算法 解决键冲突 rehas ...

  8. js数据结构-链表

    链表和数组 大家都用过js中的数组,数组其实是一种线性表的顺序存储结构,它的特点是用一组地址连续的存储单元依次存储数据元素.而它的缺点也正是其特点而造成,比如对数组做删除或者插入的时候,可能需要移动大 ...

  9. JS中数据结构之链表

    1.链表的基本介绍 数组不总是组织数据的最佳数据结构,在很多编程语言中,数组的长度是固定的,所以当数组已被数据填满时,再要加入新的元素就会非常困难.在数组中,添加和删除元素也很麻烦,因为需要将数组中的 ...

随机推荐

  1. 数据分析之matplotlib.pyplot模块

    首先都得导模块. import numpy as np import pandas as pd import matplotlib.pyplot as plt from pandas import S ...

  2. Asp.Net Web APi 路由的特点

    在ASP.NET Web API中,路由是基于HTTP协议 GET请求路由到以GET开头的控制器方法,POST请求路由到以POST开头的控制器方法中,GET方法和GetProducts,都能与GET请 ...

  3. 学代码第十七天,JAVA继承

    JANA面向对象的三大特性:封装,继承,多态. 今天学了继承,继承,通俗点说就是子类可以用父类的代码,或重写父类的方法.构造方法.属性 例如我这里要调用父类的方法: 下边有两个测试类,自己分别试一下, ...

  4. ajax和axios、fetch的区别

    参考文章: https://www.jianshu.com/p/8bc48f8fde75 Fetch API是新的ajax解决方案,用于解决古老的XHR对象不能实现的问题. fetch是用来取代传统的 ...

  5. (爬虫)urllib库

    一.爬虫简介 什么是爬虫?通俗来讲爬虫就是爬取网页数据的程序. 要了解爬虫,还需要了解HTTP协议和HTTPS协议:HTTP协议是超文本传输协议,是一种发布和接收HTML页面的传输协议:HTTPS协议 ...

  6. MySQL系统变量sql_safe_updates总结

    MySQL系统变量sql_safe_updates总结   在MySQL中,系统变量sql_safe_updates是个非常有意思的系统变量,在Oracle和SQL Server中都没有见过这样的参数 ...

  7. 彻底卸载注册表、流氓软件的工具Uninstall Tool

    Your Uninstaller 和Uninstall Tool都可以卸载Windows系统卸载不干净的软件和注册表驱动等 Uninstall Tool下载

  8. HDFS深度历险 之 从客户端逻辑看HDFS写入机制

    说明 除了标注之外,本文纯属原创,转载请注明出处:https://www.jianshu.com/p/ea6ef5f5b868, https://www.cnblogs.com/monkeyteng/ ...

  9. Java中String、StringBuilder、StringBuffer的区别

    常量还是变量: String是字符串常量(以final修饰符进行修饰,不可更改): StringBuilder是字符串变量 StringBuffer是字符串变量 线程安全: String无所谓线程安全 ...

  10. 转自阿里云邪-如何从小白成长为 Apache Committer?

    http://wuchong.me/blog/2019/02/12/how-to-become-apache-committer/ 过去三年,我一直在为 Apache Flink 开源项目贡献,也在两 ...