前言

人生总是直向前行走,从不留下什么。

原文地址:学习javascript数据结构(二)——链表

博主博客地址:Damonare的个人博客

正文

链表简介

    上一篇博客-学习javascript数据结构(一)——栈和队列说了栈和队列在javascript中的实现,我们运用javascript提供的API很容易的实现了栈和队列,但这种数据结构有一个很明显的缺点,因为数组大小是固定的所以我们在移除或是添加一项数据的时候成本很高,基本都需要吧数据重排一次。(javascript的Array类方法虽然很方便但背后的原理同样是这样的)

    相比数组我们今天主角——链表就要来的随性的多,简单的理解可以是这样:在内存中,栈和队列(数组)的存在就是一个整体,如果想要对她内部某一个元素进行移除或是添加一个新元素就要动她内部所有的元素,所谓牵一发而动全身;而链表则不一样,每一个元素都是由元素本身数据和指向下一个元素的指针构成,所以添加或是移除某一个元素不需要对链表整体进行操作,只需要改变相关元素的指针指向就可以了。

    链表在实际生活中的例子也有很多,比如自行车的链条,环环相扣,但添加或是移除某一个环节只需要对症下药,对相关环节进行操作就OK。再比如:火车,火车就是一个链表,每一节车厢就是元素,想要移除或是添加某一节车厢,只需要把连接车厢的链条改变一下就好了。那么,在javascript中又该怎么去实现链表结构呢?

链表的创建

首先我们要创建一个链表类:

function LinkedList(){
//各种属性和方法的声明
}

然后我们需要一种数据结构来保存链表里面的数据:

var Node=function(element){
this.element=element;
this.next=null;
}
//Node类表示要添加的元素,他有两个属性,一个是element,表示添加到链表中的具体的值;另一个是next,表示要指向链表中下一个元素的指针。

接下来,我们需要给链表声明一些方法:

  • append(element):向链表尾部添加一个新的元素;
  • insert(position,element):向链表特定位置插入元素;
  • remove(element):从链表移除一项;
  • indexOf(element):返回链表中某元素的索引,如果没有返回-1;
  • removeAt(position):从特定位置移除一项;
  • isEmpty():判断链表是否为空,如果为空返回true,否则返回false;
  • size():返回链表包含的元素个数;
  • toString():重写继承自Object类的toString()方法,因为我们使用了Node类;

链表的完整代码:

function LinkedList() {
//Node类声明
let Node = function(element){
this.element = element;
this.next = null;
};
//初始化链表长度
let length = 0;
//初始化第一个元素
let head = null;
this.append = function(element){
//初始化添加的Node实例
let node = new Node(element),
current;
if (head === null){
//第一个Node实例进入链表,之后在这个LinkedList实例中head就不再是null了
head = node;
} else {
current = head;
//循环链表知道找到最后一项,循环结束current指向链表最后一项元素
while(current.next){
current = current.next;
}
//找到最后一项元素后,将他的next属性指向新元素node,j建立链接
current.next = node;
}
//更新链表长度
length++;
};
this.insert = function(position, element){
//检查是否越界,超过链表长度或是小于0肯定不符合逻辑的
if (position >= 0 && position <= length){
let node = new Node(element),
current = head,
previous,
index = 0;
if (position === 0){
//在第一个位置添加
node.next = current;
head = node;
} else {
//循环链表,找到正确位置,循环完毕,previous,current分别是被添加元素的前一个和后一个元素
while (index++ < position){
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
//更新链表长度
length++;
return true;
} else {
return false;
}
};
this.removeAt = function(position){
//检查是否越界,超过链表长度或是小于0肯定不符合逻辑的
if (position > -1 && position < length){
let current = head,
previous,
index = 0;
//移除第一个元素
if (position === 0){
//移除第一项,相当于head=null;
head = current.next;
} else {
//循环链表,找到正确位置,循环完毕,previous,current分别是被添加元素的前一个和后一个元素
while (index++ < position){
previous = current;
current = current.next;
}
//链接previous和current的下一个元素,也就是把current移除了
previous.next = current.next;
}
length--;
return current.element;
} else {
return null;
}
};
this.indexOf = function(element){
let current = head,
index = 0;
//循环链表找到元素位置
while (current) {
if (element === current.element) {
return index;
}
index++;
current = current.next;
}
return -1;
};
this.remove = function(element){
//调用已经声明过的indexOf和removeAt方法
let index = this.indexOf(element);
return this.removeAt(index);
};
this.isEmpty = function() {
return length === 0;
};
this.size = function() {
return length;
};
this.getHead = function(){
return head;
};
this.toString = function(){
let current = head,
string = '';
while (current) {
string += current.element + (current.next ? ', ' : '');
current = current.next;
}
return string;
};
this.print = function(){
console.log(this.toString());
};
}
//一个实例化后的链表,里面是添加的数个Node类的实例

ES6版本:

let LinkedList2 = (function () {
class Node {
constructor(element){
this.element = element;
this.next = null;
}
}
//这里我们使用WeakMap对象来记录长度状态
const length = new WeakMap();
const head = new WeakMap();
class LinkedList2 {
constructor () {
length.set(this, 0);
head.set(this, null);
}
append(element) {
let node = new Node(element),
current;
if (this.getHead() === null) {
head.set(this, node);
} else {
current = this.getHead();
while (current.next) {
current = current.next;
}
current.next = node;
}
let l = this.size();
l++;
length.set(this, l);
}
insert(position, element) {
if (position >= 0 && position <= this.size()) { let node = new Node(element),
current = this.getHead(),
previous,
index = 0;
if (position === 0) {
node.next = current;
head.set(this, node);
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
let l = this.size();
l++;
length.set(this, l);
return true;
} else {
return false;
}
}
removeAt(position) {
if (position > -1 && position < this.size()) {
let current = this.getHead(),
previous,
index = 0;
if (position === 0) {
head.set(this, current.next);
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
let l = this.size();
l--;
length.set(this, l);
return current.element;
} else {
return null;
}
}
remove(element) {
let index = this.indexOf(element);
return this.removeAt(index);
}
indexOf(element) {
let current = this.getHead(),
index = 0;
while (current) {
if (element === current.element) {
return index;
}
index++;
current = current.next;
}
return -1;
}
isEmpty() {
return this.size() === 0;
}
size() {
return length.get(this);
}
getHead() {
return head.get(this);
}
toString() {
let current = this.getHead(),
string = '';
while (current) {
string += current.element + (current.next ? ', ' : '');
current = current.next;
}
return string; }
print() {
console.log(this.toString());
}
}
return LinkedList2;
})();

双向链表

function DoublyLinkedList() {
let Node = function(element){
this.element = element;
this.next = null;
this.prev = null; //NEW
};
let length = 0;
let head = null;
let tail = null; //NEW
this.append = function(element){
let node = new Node(element),
current;
if (head === null){
head = node;
tail = node; //NEW
} else {
//NEW
tail.next = node;
node.prev = tail;
tail = node;
}
length++;
};
this.insert = function(position, element){
if (position >= 0 && position <= length){
let node = new Node(element),
current = head,
previous,
index = 0;
if (position === 0){
if (!head){ //NEW
head = node;
tail = node;
} else {
node.next = current;
current.prev = node; //NEW
head = node;
}
} else if (position === length) { ////NEW
current = tail;
current.next = node;
node.prev = current;
tail = node;
} else {
while (index++ < position){
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
current.prev = node; //NEW
node.prev = previous; //NEW
}
length++;
return true;
} else {
return false;
}
};
this.removeAt = function(position){
if (position > -1 && position < length){
let current = head,
previous,
index = 0;
if (position === 0){ //NEW
if (length === 1){ //
tail = null;
} else {
head.prev = null;
}
} else if (position === length-1){ //NEW
current = tail;
tail = current.prev;
tail.next = null;
} else {
while (index++ < position){
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous; //NEW
}
length--;
return current.element;
} else {
return null;
}
};
this.remove = function(element){
let index = this.indexOf(element);
return this.removeAt(index);
};
this.indexOf = function(element){
let current = head,
index = -1;
if (element == current.element){
return 0;
}
index++;
while(current.next){
if (element == current.element){
return index;
}
current = current.next;
index++;
}
//check last item
if (element == current.element){
return index;
}
return -1;
};
this.isEmpty = function() {
return length === 0;
};
this. size = function() {
return length;
};
this.toString = function(){
let current = head,
s = current ? current.element : '';
while(current && current.next){
current = current.next;
s += ', ' + current.element;
}
return s;
};
this.inverseToString = function() {
let current = tail,
s = current ? current.element : '';
while(current && current.prev){
current = current.prev;
s += ', ' + current.element;
}
return s;
};
this.print = function(){
console.log(this.toString());
};
this.printInverse = function(){
console.log(this.inverseToString());
};
this.getHead = function(){
return head;
};
this.getTail = function(){
return tail;
}
}

    双向链表和单项比起来就是Node类多了一个prev属性,也就是每一个node不仅仅有一个指向它后面元素的指针也有一个指向它前面的指针。

循环链表

    明白了前面的基础链表和双向链表之后这个肯定不在话下了,循环,其实就是整个链表实例变成了一个圈,在单项链表中最后一个元素的next属性为null,现在让它指向第一个元素也就是head,那么他就成了单向循环链表。在双向链表中最后一个元素的next属性为null,现在让它指向第一个元素也就是head,那么他就成了双向循环链表。就那么回事...

后记

说到现在一直都是线性表,就是顺序数据结构,他们都是有顺序的,数据都是一条绳子上的蚂蚱。那么,如果数据是没有顺序的呢?那又该使用哪种数据结构呢?这个放到[学习javascript数据结构(三)——集合]中学习。

学习javascript数据结构(二)——链表的更多相关文章

  1. 学习javascript数据结构(三)——集合

    前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合. 原文博客地址:学习javascript数据结构(三)--集合 知乎专栏&&简书专题:前端进击者 ...

  2. 学习javascript数据结构(四)——树

    前言 总括: 本文讲解了数据结构中的[树]的概念,尽可能通俗易懂的解释树这种数据结构的概念,使用javascript实现了树,如有纰漏,欢迎批评指正. 原文博客地址:学习javascript数据结构( ...

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

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

  4. 学习JavaScript数据结构与算法 (二)

    学习JavaScript数据结构与算法 的笔记 包含第四章队列, 第五章链表 本人所有文章首发在博客园: http://www.cnblogs.com/zhangrunhao/ 04队列 实现基本队列 ...

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

    定场诗 大将生来胆气豪,腰横秋水雁翎刀. 风吹鼍鼓山河动,电闪旌旗日月高. 天上麒麟原有种,穴中蝼蚁岂能逃. 太平待诏归来日,朕与先生解战袍. 此处应该有掌声... 前言 读<学习JavaScr ...

  6. 学习javascript数据结构(一)——栈和队列

    前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...

  7. 学习JavaScript数据结构与算法 (一)

    学习JavaScript数据结构与算法 的笔记, 包含一二三章 01基础 循环 斐波那契数列 var fibonaci = [1,1] for (var i = 2; i< 20;i++) { ...

  8. 重读《学习JavaScript数据结构与算法-第三版》-第2章 ECMAScript与TypeScript概述

    定场诗 八月中秋白露,路上行人凄凉: 小桥流水桂花香,日夜千思万想. 心中不得宁静,清早览罢文章, 十年寒苦在书房,方显才高志广. 前言 洛伊安妮·格罗纳女士所著的<学习JavaScript数据 ...

  9. 重读《学习JavaScript数据结构与算法-第三版》- 第4章 栈

    定场诗 金山竹影几千秋,云索高飞水自流: 万里长江飘玉带,一轮银月滚金球. 远自湖北三千里,近到江南十六州: 美景一时观不透,天缘有分画中游. 前言 本章是重读<学习JavaScript数据结构 ...

随机推荐

  1. 隐马尔科夫模型python实现简单拼音输入法

    在网上看到一篇关于隐马尔科夫模型的介绍,觉得简直不能再神奇,又在网上找到大神的一篇关于如何用隐马尔可夫模型实现中文拼音输入的博客,无奈大神没给可以运行的代码,只能纯手动网上找到了结巴分词的词库,根据此 ...

  2. 引人瞩目的 CSS 变量(CSS Variable)

    这是一个令人激动的革新. CSS 变量,顾名思义,也就是由网页的作者或用户定义的实体,用来指定文档中的特定变量. 更准确的说法,应该称之为 CSS 自定义属性 ,不过下文为了好理解都称之为 CSS 变 ...

  3. 破解SQLServer for Linux预览版的3.5GB内存限制 (RHEL篇)

    微软发布了SQLServer for Linux,但是安装竟然需要3.5GB内存,这让大部分云主机用户都没办法尝试这个新东西 这篇我将讲解如何破解这个内存限制 要看关键的可以直接跳到第6步,只需要替换 ...

  4. 【知识必备】一文让你搞懂design设计的CoordinatorLayout和AppbarLayout联动,让Design设计更简单~

    一.写在前面 其实博主在之前已经对design包的各个控件都做了博文说明,无奈个人觉得理解不够深入,所以有了这篇更加深入的介绍,希望各位看官拍砖~ 二.从是什么开始 1.首先我们得知道Coordina ...

  5. JAVA程序员常用软件整理下载

    ********为了大家学习方便,特意整理软件下载如下:*************Java类软件:-------------------------------JDK7.0:http://pan.ba ...

  6. ASP.NET Core 中文文档 第四章 MVC(3.9)视图组件

    作者: Rick Anderson 翻译: 娄宇(Lyrics) 校对: 高嵩 章节: 介绍视图组件 创建视图组件 调用视图组件 演练:创建一个简单的视图组件 附加的资源 查看或下载示例代码 介绍视图 ...

  7. 计算Div标签内Checkbox个数或已被disabled的个数

    先看下面的html: 计算div内的checkbox个数:$('#divmod input[type="checkbox"]').length 计算div内checkbox被dis ...

  8. 升级npm

    查看npm的所有版本 运行命令: npm view npm versions 命令运行后,会输出到目前为止npm的所有版本. [ '1.1.25', '1.1.70', '1.1.71', '1.2. ...

  9. BAT“搅局”B2B市场,CIO们准备好了吗?

    "CIO必须灵活构建其所在企业的IT系统,深入业务,以应对日新月异的数字化业务环境."   BAT军团"搅局"B2B市场,CIO们准备好了吗? 庞大的企业级市场 ...

  10. git和pycharm管理代码

    首先明白三个概念,服务器代码库,本地代码库,和正在coding的项目. coding完毕后,先通过commit提交到本地代码库,然后通过push再提交server的代码库    git步骤 git c ...