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

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

TypeScript方式实现源码

 class Node {
element;
next;
constructor(element) {
this.element = element;
this.next = null;
}
}
class LinkedList {
private length = ;
head = null; constructor() {
}
/**
* 向列表尾部添加一个新的项
* @param element
*/
public append(element) {
let node = new Node(element), current; if (this.head === null) {
this.head = node;
} else {
current = this.head; // 循环列表,知道找到最后一项
while (current.next) {
current = current.next;
} // 找到最后一项,将其next赋为node,建立链接
current.next = node;
}
this.length++; // 更新列表长度
}
/**
* 向列表的特定位置插入一个新的项
* @param position
* @param element
*/
public insert(position, element) {
// 检查越界值
if (position >= && position <= length) {
let node = new Node(element),
current = this.head,
previous,
index = ; if (position === ) { // 在第一个位置添加
node.next = current;
this.head = node;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
this.length++; // 更新列表长度
return true;
} else {
return false;
}
}
/**
* 从列表的特定位置移除一项
* @param position
*/
public removeAt(position) {
// 检查越界值
if (position > - && position < this.length) {
let current = this.head,
previous,
index = ; // 移除第一项
if (position === ) {
this.head = current.next;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
// 将previous与current的下一项链接起来:跳过current,从而移除它
previous.next = current.next;
}
this.length--;
return current.element;
} else {
return null;
}
}
/**
* 从列表中移除一项
* @param element
*/
public remove(element) {
let index = this.indexOf(element);
return this.removeAt(index);
}
/**
* :返回元素在列表中的索引。如果列表中没有该元素则返回-1
* @param element
*/
public indexOf(element) {
let current = this.head,
index = -; while (current) {
if (element === current.element) {
return index;
}
index++;
current = current.next;
}
return -;
}
/**
* 如果链表中不包含任何元素, 返回true, 如果链表长度大于0则返回false
*/
public isEmpty() {
return this.length === ;
}
/**
* 返回链表包含的元素个数。与数组的length属性类似
*/
public size() {
return this.length;
}
/**
* 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值
*/
public toString() {
let current = this.head,
string = ''; while (current) {
string += current.element;
current = current.next;
}
return string;
}
public getHead() {
return this.head;
}
public print() {
console.log(this.toString());
}
}
// 代码解读:
// 充分利用引用对象特性,将需要管理的数据加入链表的数据结构中,
// 完全不依赖系统数组的数据结构,所以避免大量删、增数据数组排序的性能损耗。同时也失去了数组下标取数据的优势。
// 因此优势劣势非常明显,大量改删数据场景选用链表,大量已知下标去更新数据选用数组 // 抽象:
// 火车车厢可长可短,随时加入或去掉一节车厢仅仅对操作车厢前后一节有改动,当车厢非常长的时候,这个优势尤为明显,
// 这仅仅是一种应用场景也可以通过火车的例子去理解这种数据结构的设计理念 // 总结:
// 链表以引用作为媒介,将大量不相干的数据进行一个有序的链接,同时没有复杂的关联关系,仅仅关系操作数据前后关联数据
// 面对大量增加删除的场景,链表将是比数组更好的选择
JavaScript方式实现源码
 var Node = (function () {
function Node(element) {
this.element = element;
this.next = null;
}
return Node;
}());
var LinkedList = (function () {
function LinkedList() {
this.length = ;
this.head = null;
}
/**
* 向列表尾部添加一个新的项
* @param element
*/
LinkedList.prototype.append = function (element) {
var node = new Node(element), current;
if (this.head === null) {
this.head = node;
}
else {
current = this.head;
// 循环列表,知道找到最后一项
while (current.next) {
current = current.next;
}
// 找到最后一项,将其next赋为node,建立链接
current.next = node;
}
this.length++; // 更新列表长度
};
/**
* 向列表的特定位置插入一个新的项
* @param position
* @param element
*/
LinkedList.prototype.insert = function (position, element) {
// 检查越界值
if (position >= && position <= length) {
var node = new Node(element), current = this.head, previous = void , index = ;
if (position === ) {
node.next = current;
this.head = node;
}
else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
this.length++; // 更新列表长度
return true;
}
else {
return false;
}
};
/**
* 从列表的特定位置移除一项
* @param position
*/
LinkedList.prototype.removeAt = function (position) {
// 检查越界值
if (position > - && position < this.length) {
var current = this.head, previous = void , index = ;
// 移除第一项
if (position === ) {
this.head = current.next;
}
else {
while (index++ < position) {
previous = current;
current = current.next;
}
// 将previous与current的下一项链接起来:跳过current,从而移除它
previous.next = current.next;
}
this.length--;
return current.element;
}
else {
return null;
}
};
/**
* 从列表中移除一项
* @param element
*/
LinkedList.prototype.remove = function (element) {
var index = this.indexOf(element);
return this.removeAt(index);
};
/**
* :返回元素在列表中的索引。如果列表中没有该元素则返回-1
* @param element
*/
LinkedList.prototype.indexOf = function (element) {
var current = this.head, index = -;
while (current) {
if (element === current.element) {
return index;
}
index++;
current = current.next;
}
return -;
};
/**
* 如果链表中不包含任何元素, 返回true, 如果链表长度大于0则返回false
*/
LinkedList.prototype.isEmpty = function () {
return this.length === ;
};
/**
* 返回链表包含的元素个数。与数组的length属性类似
*/
LinkedList.prototype.size = function () {
return this.length;
};
/**
* 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值
*/
LinkedList.prototype.toString = function () {
var current = this.head, string = '';
while (current) {
string += current.element;
current = current.next;
}
return string;
};
LinkedList.prototype.getHead = function () {
return this.head;
};
LinkedList.prototype.print = function () {
console.log(this.toString());
};
return LinkedList;
}());

JavaScript数据结构与算法(六) 链表的实现的更多相关文章

  1. JavaScript 数据结构与算法3(链表)

    学习数据结构的 git 代码地址: https://gitee.com/zhangning187/js-data-structure-study 1.链表 本章学习如何实现和使用链表这种动态的数据结构 ...

  2. 为什么我要放弃javaScript数据结构与算法(第六章)—— 集合

    前面已经学习了数组(列表).栈.队列和链表等顺序数据结构.这一章,我们要学习集合,这是一种不允许值重复的顺序数据结构. 本章可以学习到,如何添加和移除值,如何搜索值是否存在,也可以学习如何进行并集.交 ...

  3. 为什么我要放弃javaScript数据结构与算法(第五章)—— 链表

    这一章你将会学会如何实现和使用链表这种动态的数据结构,这意味着我们可以从中任意添加或移除项,它会按需进行扩张. 本章内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 类 ...

  4. JavaScript数据结构与算法-链表练习

    链表的实现 一. 单向链表 // Node类 function Node (element) { this.element = element; this.next = null; } // Link ...

  5. 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)

    定场诗 伤情最是晚凉天,憔悴厮人不堪言: 邀酒摧肠三杯醉.寻香惊梦五更寒. 钗头凤斜卿有泪,荼蘼花了我无缘: 小楼寂寞新雨月.也难如钩也难圆. 前言 本章为重读<学习JavaScript数据结构 ...

  6. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  7. javascript数据结构与算法 零(前记+前言)

    前记 这本书Data Structure and Algorithm with Javascript 我将其翻译成<< javascript 数据结构和算法>> 为什么这么翻译 ...

  8. 为什么我要放弃javaScript数据结构与算法(第九章)—— 图

    本章中,将学习另外一种非线性数据结构--图.这是学习的最后一种数据结构,后面将学习排序和搜索算法. 第九章 图 图的相关术语 图是网络结构的抽象模型.图是一组由边连接的节点(或顶点).学习图是重要的, ...

  9. 为什么我要放弃javaScript数据结构与算法(第八章)—— 树

    之前介绍了一些顺序数据结构,介绍的第一个非顺序数据结构是散列表.本章才会学习另一种非顺序数据结构--树,它对于存储需要快速寻找的数据非常有用. 本章内容 树的相关术语 创建树数据结构 树的遍历 添加和 ...

随机推荐

  1. xml解析多个结点方法(C#)

    解析多个结点的XML文件,格式如下: <?xml version="1.0" encoding="utf-8"?> <response> ...

  2. [css 实践篇] 解决悬浮的<header> <footer>遮挡内容的处理技巧

    我写的实践篇 都是自己在实践项目所遇到的 "拦路虎" 还是很有借鉴的意义的.(实践才是检验真理的唯一标准呀),废话不多说,进去正题 position: fixed 绝对固定底部后会 ...

  3. java 打印近似圆

    只要给定不同半径,圆的大小就会随之发生改变近似圆如图 设半径为r,圆心为(r,r). 由勾股定理 易得y = r -√(2*r*x-x*x) 此处注意x+=2.因为打印窗口上一行2个字节的宽度与一列的 ...

  4. Matlab绘图基础——绘制向量图,二维三维(绘制参数曲线图)

    ------------------------------------------- %绘制向量场图 %例一 clear all;clc; [X,Y] = meshgrid(-2:.2:2,-3:. ...

  5. selenium2自动化测试学习笔记(五)-参数化编程,自动登陆网易QQ邮箱

    学习python下使用selenium2自动测试第6天,参数化编程这节课花了两天时间. 本次编程主要时间是花在熟悉python上 知识点or坑点: 1.读取txt.xml.csv等文件存储的账号.密码 ...

  6. Jquery瀑布流布局,jQuery Wookmark Load 示例

    瀑布流布局非常适合大量图片的展示,一改过去裁剪图片尺寸同意的排版,每张图片都能完全展示,并错落有致,让人眼前一亮. 注意事项:img元素的width和weight属性需要写,否则定位会不准确. 查看j ...

  7. PTA的使用简介

    PTA(Programming Teaching Assistant)是PAT(Programming Ability Test)的配套练习平台. 1.关于PAT PAT(Programming Ab ...

  8. Alpha冲刺No.7

    一.站立式会议 彻底完成初步的界面设计 实现界面的简单跳转 完成部分事件监听 移植摄像头.图库功能到真实手机环境测试 数据库上传获取日记 二.项目实际进展 完成了简单的界面设计 大致完成了跳转任务 数 ...

  9. 学号:201621123032 《Java程序设计》第11周学习总结

    1:本周学习总结 1.1.:以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2:书面作业 2.1.: 源代码阅读:多线程程序BounceThread 1.1: BallRunnable类有什 ...

  10. Linux下硬盘分区

    1  fdisk -l查看硬盘及分区信息 我的系统(Archlinux)下的命令效果如下: 由上面的图片可以得知该系统只挂载了1个硬盘,命名为sda,其有2个主分区,sda1和sda2,至于为什么这么 ...