JavaScript数据结构——链表
|
序号
|
方法
|
说明
|
|
1
|
append(element) | 向列表尾部添加一个新的项 |
|
2
|
insert(position, element) | 向列表的特定位置插入一个新的项 |
|
3
|
remove(element)
|
从列表中移除一项 |
|
4
|
indexOf (element )
|
返回元素在列表中的索引。如果列表中没有该元素则返回-1 |
|
5
|
removeAt(position) |
从列表的特定位置移除一项
|
|
6
|
isEmpty()
|
如果链表中不包含任何元素,返回 true,如果链表长度大于 0 则返回 false
|
|
7
|
size ( )
|
返回链表包含元素个数。与数组的 length 属性类似 |
|
8
|
toString ( )
|
由于列表项使用了 Node 类,就需要重写继承自 JavaScript 对象默认的 toString 方法,让其只输出元素的值 |
链表实现:
function LinkedList() {
// 定义辅助类Node
var Node = function(element) {
this.element = element; // element属性,即要添加到列表的元素
this.next = null; // next属性,即指向列表中下一个节点项的指针
}
var length = 0; // 内部属性/私有变量
var head = null; // 第一个节点的引用
// 向列表的尾部添加一个新的项
this.append = function(element) {
var node = new Node(element), // 把element作为值传入,创建Node项
current;
if (head === null) { // 列表中第一个节点,如果head元素为null,则意味着向列表添加第一个元素
head = node; // head指向node元素,下一个node将会自动生成null
} else {
current = head;
// 循环列表,直到找到最后一项
while(current.next) {
current = current.next;
}
// 找到最后一项,将其next赋为node,建立连接
current.next = node; // 列表中最后一个节点的下一个元素始终是null
}
length++; // 更新链表长度,这样就能控制它,轻松地得到列表的长度
};
// 向列表的特定位置插入一个新的项
this.insert = function(position, element) {
// 检查越界值
if (position >= 0 && position <= length) {
var node = new Node(element),
current = head,
previous,
index = 0;
if (position === 0) { // 在第一个位置添加
node.next = current;
head = node;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
}
length++;
return true;
} else {
return false;
}
};
// 从列表的特定位置移除一项
this.removeAt = function(position) {
// 检查越界值
if (position > -1 && position < length) {
var current = head, // current变量总是为对所循环列表的当前元素的引用
previous, // previous变量为对当前元素的前一个元素的引用
index = 0;
// 移除第一项
if (position === 0) {
head = current.next;
} else {
while (index++ < position) { // 使用用于内部控制和递增的index变量来迭代列表
previous = current;
current = current.next;
}
// 将previous与current的下一项链接起来:跳过current,从而移除它
previous.next = current.next;
}
length--;
return current.element;
} else {
return null;
}
};
// 从列表中移除一项
this.remove = function(element) {
var index = this.indexOf(element);
return this.removeAt(index);
};
// 返回元素在列表中的索引。如果列表中没有该元素则返回-1
this.indexOf = function(element) {
var current = head,
index = -1;
while (current) {
if (element === current.element) {
return index;
}
index++;
current = current.next;
}
return -1;
};
// 如果链表中不包含任何元素,返回 true,如果链表长度大于 0 则返回 false
this.isEmpty = function() {
return length === 0;
};
// 返回链表包含元素个数。与数组的 length 属性类似
this.size = function() {
return length;
};
// 由于列表项使用了 Node 类,就需要重写继承自 JavaScript 对象默认的 toString 方法,让其只输出元素的值
this.toString = function() {
var current = head,
string = '';
while (current) {
string += current.element;
current = current.next;
}
return string;
};
this.getHead = function () {
return head;
};
}
LinkedList.js
function DoublyLinkedList() {
var Node = function(element) {
this.element = element;
this.next = null;
this.prev = null;
};
var length = 0;
var head = null;
var tail = null; // 新增的
// 特定位置插入元素
this.insert = function(position, element) {
// 检查越界值
if (position >= 0 && position <= length) {
var node = new Node(element),
current = head,
previous,
index = 0;
if (position === 0) { // 在第一个位置添加
if (!head) { // 新增的
head = node;
tail = node;
} else {
node.next = current;
current.prev = node;
head = node;
}
} else if (position === length-1) { // 最后一项 //新增的
current = tail;
current.next = node;
node.prev = current;
tail = node;
} else {
while (index++ < position) {
previous = current;
previous.next = node;
}
node.next = current;
previous.next = node;
current.prev = node; //新增的
node.prev = previous; //新增的
}
length++;
return true;
} else {
return false;
}
};
// 特定位置删除元素
this.removeAt = function(position) {
// 检查越界值
if (position > -1 && position < length) {
var current = head,
previous,
index = 0;
// 移除第一项
if (position === 0) {
head = current.next;
// 如果只有一项,更新tail //新增的
if (length === 1) {
tail = null;
} else {
head.prev = null;
}
} else if (position === length-1) {
current = tail;
tail = current.prev;
tail.next = null;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
// 将prvious与current的下一项链接起来——跳过current
previous.next = current.next;
current.next.prev = previous; // 新增的
}
length--;
return current.element;
} else {
return null;
}
};
}
DoublyLinkedList.js
参考书籍:《学习JavaScript数据结构与算法》
JavaScript数据结构——链表的更多相关文章
- JavaScript数据结构——链表的实现
前面楼主分别讨论了数据结构栈与队列的实现,当时所用的数据结构都是用的数组来进行实现,但是数组有的时候并不是最佳的数据结构,比如在数组中新增删除元素的时候需要将其他元素进行移动,而在javascript ...
- JavaScript数据结构——链表的实现与应用
链表用来存储有序的元素集合,与数组不同,链表中的元素并非保存在连续的存储空间内,每个元素由一个存储元素本身的节点和一个指向下一个元素的指针构成.当要移动或删除元素时,只需要修改相应元素上的指针就可以了 ...
- javascript数据结构-链表
gihtub博客地址 链表 是一种物理存储单元上非连续.非顺序的存储结构,它既可以表示线性结构,也可以用于表示非线性结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每 ...
- JavaScript数据结构——字典和散列表的实现
在前一篇文章中,我们介绍了如何在JavaScript中实现集合.字典和集合的主要区别就在于,集合中数据是以[值,值]的形式保存的,我们只关心值本身:而在字典和散列表中数据是以[键,值]的形式保存的,键 ...
- JavaScript数据结构——树的实现
在计算机科学中,树是一种十分重要的数据结构.树被描述为一种分层数据抽象模型,常用来描述数据间的层级关系和组织结构.树也是一种非顺序的数据结构.下图展示了树的定义: 在介绍如何用JavaScript实现 ...
- 学习javascript数据结构(二)——链表
前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...
- 为什么我要放弃javaScript数据结构与算法(第五章)—— 链表
这一章你将会学会如何实现和使用链表这种动态的数据结构,这意味着我们可以从中任意添加或移除项,它会按需进行扩张. 本章内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 类 ...
- JavaScript数据结构与算法-链表练习
链表的实现 一. 单向链表 // Node类 function Node (element) { this.element = element; this.next = null; } // Link ...
- 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)
定场诗 伤情最是晚凉天,憔悴厮人不堪言: 邀酒摧肠三杯醉.寻香惊梦五更寒. 钗头凤斜卿有泪,荼蘼花了我无缘: 小楼寂寞新雨月.也难如钩也难圆. 前言 本章为重读<学习JavaScript数据结构 ...
随机推荐
- 工程技巧Linux上建立工程项目
程序中用到的核心代码用库的形式进行封装,并且给出示例程序,下面给出一个程序文件夹的建立脚本. 如运行sh MakeProject.sh PersonNameIdentification PNILib ...
- su和su -和sudo
1.su和sudo没有切换工作目录和环境变量,只是赋予用户权限, 而su -是真正切换到root登录,工作目录切换到/root,环境变量也同时改变. [root@oc3408554812 home]# ...
- php安装出现的部分错误
在CentOS编译PHP5的时候有时会遇到以下的一些错误信息,基本上都可以通过yum安装相应的库来解决.以下是具体的一些解决办法: checking for BZip2 support… yes ch ...
- Halcon 映射校正例程注释(MapImage)
*关闭窗口 dev_close_window () dev_close_window () *打开指定大小.颜色背景的窗口 dev_open_window (, , /, /, 'black', Wi ...
- LA 3516 - Exploring Pyramids
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- POJ 1436 Horizontally Visible Segments
题意: 有一些平行于y轴的线段 ,两条线段称为互相可见当且仅当存在一条水平线段连接这两条 与其他线段没交点. 最后问有多少组 3条线段,他们两两是可见的. 思路: 线段树,找出两两可见的那些组合, ...
- g++默认支持c++11标准的办法
//第一种,直接包含在源程序文件中,如第一行代码所示 #pragma GCC diagnostic error "-std=c++11" #include <iostream ...
- 怎么学好python?
文章摘自:http://www.jb51.net/article/16100.htm 1)学好python的第一步,就是马上到www.python.org网站上下载一个python版本.我建议初学者, ...
- 矩阵(matrix)
我们定义一个矩阵的权值为这个矩阵四个角上的数值的最小值.现在小M有一个矩阵,他想在这个矩阵中寻找到一个权值最大的子矩阵,请你告诉他这个最大权值.(距形规模最大为2000*2000) 比赛 看到第二题那 ...
- Winform在一个窗体获取其他窗体的值
比如:Form2获取Form1 的label的值 因为默认的窗体的所有控件属性和方法都是private, Form1 form1 = new Form1(); 这样也是获取不到的 方法一.最简单的 将 ...