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

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

TypeScript方式实现源码

  1 class Node {
  2     element;
  3     next;
  4     constructor(element) {
  5         this.element = element;
  6         this.next = null;
  7     }
  8 }
  9 class LinkedList {
 10     private length = 0;
 11     head = null;
 12
 13     constructor() {
 14     }
 15     /**
 16      * 向列表尾部添加一个新的项
 17      * @param element
 18      */
 19     public append(element) {
 20         let node = new Node(element), current;
 21
 22         if (this.head === null) {
 23             this.head = node;
 24         } else {
 25             current = this.head;
 26
 27             // 循环列表,知道找到最后一项
 28             while (current.next) {
 29                 current = current.next;
 30             }
 31
 32             // 找到最后一项,将其next赋为node,建立链接
 33             current.next = node;
 34         }
 35         this.length++; // 更新列表长度
 36     }
 37     /**
 38      * 向列表的特定位置插入一个新的项
 39      * @param position
 40      * @param element
 41      */
 42     public insert(position, element) {
 43         // 检查越界值
 44         if (position >= 0 && position <= length) {
 45             let node = new Node(element),
 46                 current = this.head,
 47                 previous,
 48                 index = 0;
 49
 50             if (position === 0) { // 在第一个位置添加
 51                 node.next = current;
 52                 this.head = node;
 53             } else {
 54                 while (index++ < position) {
 55                     previous = current;
 56                     current = current.next;
 57                 }
 58                 node.next = current;
 59                 previous.next = node;
 60             }
 61             this.length++; // 更新列表长度
 62             return true;
 63         } else {
 64             return false;
 65         }
 66     }
 67     /**
 68      * 从列表的特定位置移除一项
 69      * @param position
 70      */
 71     public removeAt(position) {
 72         // 检查越界值
 73         if (position > -1 && position < this.length) {
 74             let current = this.head,
 75                 previous,
 76                 index = 0;
 77
 78             // 移除第一项
 79             if (position === 0) {
 80                 this.head = current.next;
 81             } else {
 82                 while (index++ < position) {
 83                     previous = current;
 84                     current = current.next;
 85                 }
 86                 // 将previous与current的下一项链接起来:跳过current,从而移除它
 87                 previous.next = current.next;
 88             }
 89             this.length--;
 90             return current.element;
 91         } else {
 92             return null;
 93         }
 94     }
 95     /**
 96      * 从列表中移除一项
 97      * @param element
 98      */
 99     public remove(element) {
100         let index = this.indexOf(element);
101         return this.removeAt(index);
102     }
103     /**
104      * :返回元素在列表中的索引。如果列表中没有该元素则返回-1
105      * @param element
106      */
107     public indexOf(element) {
108         let current = this.head,
109             index = -1;
110
111         while (current) {
112             if (element === current.element) {
113                 return index;
114             }
115             index++;
116             current = current.next;
117         }
118         return -1;
119     }
120     /**
121      * 如果链表中不包含任何元素, 返回true, 如果链表长度大于0则返回false
122      */
123     public isEmpty() {
124         return this.length === 0;
125     }
126     /**
127      * 返回链表包含的元素个数。与数组的length属性类似
128      */
129     public size() {
130         return this.length;
131     }
132     /**
133      * 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值
134      */
135     public toString() {
136         let current = this.head,
137             string = '';
138
139         while (current) {
140             string += current.element;
141             current = current.next;
142         }
143         return string;
144     }
145     public getHead() {
146         return this.head;
147     }
148     public print() {
149         console.log(this.toString());
150     }
151 }

View Code

// 代码解读:// 充分利用引用对象特性,将需要管理的数据加入链表的数据结构中,// 完全不依赖系统数组的数据结构,所以避免大量删、增数据数组排序的性能损耗。同时也失去了数组下标取数据的优势。// 因此优势劣势非常明显,大量改删数据场景选用链表,大量已知下标去更新数据选用数组// 抽象:// 火车车厢可长可短,随时加入或去掉一节车厢仅仅对操作车厢前后一节有改动,当车厢非常长的时候,这个优势尤为明显,// 这仅仅是一种应用场景也可以通过火车的例子去理解这种数据结构的设计理念// 总结:// 链表以引用作为媒介,将大量不相干的数据进行一个有序的链接,同时没有复杂的关联关系,仅仅关系操作数据前后关联数据// 面对大量增加删除的场景,链表将是比数组更好的选择

JavaScript方式实现源码

  1 var Node = (function () {
  2     function Node(element) {
  3         this.element = element;
  4         this.next = null;
  5     }
  6     return Node;
  7 }());
  8 var LinkedList = (function () {
  9     function LinkedList() {
 10         this.length = 0;
 11         this.head = null;
 12     }
 13     /**
 14      * 向列表尾部添加一个新的项
 15      * @param element
 16      */
 17     LinkedList.prototype.append = function (element) {
 18         var node = new Node(element), current;
 19         if (this.head === null) {
 20             this.head = node;
 21         }
 22         else {
 23             current = this.head;
 24             // 循环列表,知道找到最后一项
 25             while (current.next) {
 26                 current = current.next;
 27             }
 28             // 找到最后一项,将其next赋为node,建立链接
 29             current.next = node;
 30         }
 31         this.length++; // 更新列表长度
 32     };
 33     /**
 34      * 向列表的特定位置插入一个新的项
 35      * @param position
 36      * @param element
 37      */
 38     LinkedList.prototype.insert = function (position, element) {
 39         // 检查越界值
 40         if (position >= 0 && position <= length) {
 41             var node = new Node(element), current = this.head, previous = void 0, index = 0;
 42             if (position === 0) {
 43                 node.next = current;
 44                 this.head = node;
 45             }
 46             else {
 47                 while (index++ < position) {
 48                     previous = current;
 49                     current = current.next;
 50                 }
 51                 node.next = current;
 52                 previous.next = node;
 53             }
 54             this.length++; // 更新列表长度
 55             return true;
 56         }
 57         else {
 58             return false;
 59         }
 60     };
 61     /**
 62      * 从列表的特定位置移除一项
 63      * @param position
 64      */
 65     LinkedList.prototype.removeAt = function (position) {
 66         // 检查越界值
 67         if (position > -1 && position < this.length) {
 68             var current = this.head, previous = void 0, index = 0;
 69             // 移除第一项
 70             if (position === 0) {
 71                 this.head = current.next;
 72             }
 73             else {
 74                 while (index++ < position) {
 75                     previous = current;
 76                     current = current.next;
 77                 }
 78                 // 将previous与current的下一项链接起来:跳过current,从而移除它
 79                 previous.next = current.next;
 80             }
 81             this.length--;
 82             return current.element;
 83         }
 84         else {
 85             return null;
 86         }
 87     };
 88     /**
 89      * 从列表中移除一项
 90      * @param element
 91      */
 92     LinkedList.prototype.remove = function (element) {
 93         var index = this.indexOf(element);
 94         return this.removeAt(index);
 95     };
 96     /**
 97      * :返回元素在列表中的索引。如果列表中没有该元素则返回-1
 98      * @param element
 99      */
100     LinkedList.prototype.indexOf = function (element) {
101         var current = this.head, index = -1;
102         while (current) {
103             if (element === current.element) {
104                 return index;
105             }
106             index++;
107             current = current.next;
108         }
109         return -1;
110     };
111     /**
112      * 如果链表中不包含任何元素, 返回true, 如果链表长度大于0则返回false
113      */
114     LinkedList.prototype.isEmpty = function () {
115         return this.length === 0;
116     };
117     /**
118      * 返回链表包含的元素个数。与数组的length属性类似
119      */
120     LinkedList.prototype.size = function () {
121         return this.length;
122     };
123     /**
124      * 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值
125      */
126     LinkedList.prototype.toString = function () {
127         var current = this.head, string = '';
128         while (current) {
129             string += current.element;
130             current = current.next;
131         }
132         return string;
133     };
134     LinkedList.prototype.getHead = function () {
135         return this.head;
136     };
137     LinkedList.prototype.print = function () {
138         console.log(this.toString());
139     };
140     return LinkedList;
141 }());

View Code

转载于:https://www.cnblogs.com/menu/p/6971152.html

JavaScript数据结构与算法(六) 链表的实现相关推荐

  1. JavaScript数据结构与算法(1)(数组、栈、队列、链表)(ES6)

    注意:原教学视频:JavaScript(ES6)数据结构和算法 | JavaScript数据结构与算法 (都是CoderWhy老师的教学) 原作者(笔记)链接:JavaScript 数据结构与算法 | ...

  2. JavaScript数据结构与算法——链表详解(下)

    在JavaScript数据结构与算法--链表详解(上)中,我们探讨了一下链表的定义.实现原理以及单链表的实现.接下来我们进一步了解一下链表的其他内容. 1.双向链表 双向链表实现原理图: 与单向链表不 ...

  3. JavaScript数据结构与算法——链表详解(上)

    注:与之前JavaScript数据结构与算法系列博客不同的是,从这篇开始,此系列博客采用es6语法编写,这样在学数据结构的同时还能对ECMAScript6有进一步的认识,如需先了解es6语法请浏览ht ...

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

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

  5. JavaScript数据结构与算法(2)(集合、字典、哈希表、二叉树、图)(ES6)

    注意:原教学视频:JavaScript(ES6)数据结构和算法 | JavaScript数据结构与算法 (都是CoderWhy老师的教学) 原作者(笔记)链接:JavaScript 数据结构与算法 | ...

  6. 学习JavaScript数据结构与算法(一):栈与队列

    本系列的第一篇文章: 学习JavaScript数据结构与算法(一),栈与队列 第二篇文章:学习JavaScript数据结构与算法(二):链表 第三篇文章:学习JavaScript数据结构与算法(三): ...

  7. 为什么我要放弃javaScript数据结构与算法(第二章)—— 数组

    第二章 数组 几乎所有的编程语言都原生支持数组类型,因为数组是最简单的内存数据结构.JavaScript里也有数组类型,虽然它的第一个版本并没有支持数组.本章将深入学习数组数据结构和它的能力. 为什么 ...

  8. JavaScript数据结构和算法简述——数组

    为什么先讲数组 数据结构可以简单的被分为线性结构和非线性结构. 线性结构大致包括: 数组(连续存储): 链表(离散存储): 栈(线性结构常见应用,由链表或数组增删和改进功能实现): 队列(线性结构常见 ...

  9. JavaScript数据结构和算法简述——前言

    为什么要使用数据结构和算法(程序=数据结构+算法)         数据结构是对在计算机内存中(有时在磁盘中)的数据的一种安排.包括数组.链表.栈.二叉树.哈希表等.        算法是对这些结构中 ...

最新文章

  1. IOS 百度地图获取当前屏幕的经纬度
  2. c语言scanf结果在printf前,C语言中的scanf与printf
  3. 数据标准化(归一化)
  4. Druid runningSqlCount 1 线上解决思路
  5. python的数值可以转换为字符串_python 数值转换为字符串Python对HTML转义字符进行反转义...
  6. (转)Linux内核参数之arp_ignore和arp_announce
  7. 【转】Jenkins怎么启动和停止服务
  8. Vue.js(2.x)之插值
  9. 【手势识别】基于matlab GUI肤色手势识别【含Matlab源码 716期】
  10. mysql误操作删除数据后数据恢复
  11. 408计算机考试科目英语数学,关于计算机考研408的那些事儿
  12. 带通滤波器作用和用途_什么是带通滤波器?工作原理及原理图详解
  13. linux head
  14. 前女友让我撸个植物大战僵尸,我一怒之下把代码开源了...
  15. Linux系统管理---权限管理
  16. Vue的patch算法(了解)
  17. 算法的时间复杂度表示法(大O表示法)
  18. 稻盛和夫的人生法则,所谓人生赢家,不过是拥有“利他心”
  19. 期望、方差、协方差性质总结与证明
  20. win7计算机建立无线网络连接不上,教你电脑连接不上无线网络怎么办

热门文章

  1. Jzoj5245 Competing Souls
  2. 又一家药企IPO被拒,原因竟然是……
  3. 清除sqlserver日志方法(不适合always on)
  4. 我学习设计模式的一些所想所得
  5. 新发现的两个Delphi要点。
  6. Asp.net输出Excel文件并且下载该文件以及某些细节问题解决
  7. linux内核数据结构实现--链表、队列和哈希
  8. Nginx之代理和负载均衡
  9. (74)信号发生器DDS三角波设计(二)(第15天)
  10. (73)FPGA模块调用(VHDL调用system Verilog)