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数据结构 ...
随机推荐
- POJ 3206 最小生成树
DESCRIPTION:T_T 在下是读不懂题意的.但是捏.现在知道是求把所有的点(是字母的点)连起来的最小的权值.即最小生成树.因为求最小生成树是不计较源点是哪个的.所以可以把A和S看成一样的.首先 ...
- 用C#用C#实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码
run(){ while(true) { show(); if(条件) { ...
- 浅谈C++源码的过国内杀软的免杀
以下只是简单的思路和定位.也许有人秒过,但是不要笑话我写的笨方法.定位永远是过期不了的. 其实这里废话一下 , 本人并不是大牛 ,今天跟大家分享下 .所以写出这篇文章.(大牛飘过) 只是个人实战的经验 ...
- Android 解压html压缩数据
public static String unzipHTML(String s){ int endPos = s.indexOf("\r\n\r\n"); if(endPos< ...
- POJ 1850 Code 字符串 难度:1
题意: 1 如果是严格升序的字母字符串,那么可以输出非0解码,否则不能译码输出0 2 字符串解码 遵循递增原则,其值为 到现在为止的所有按字母序小于该字符串的数量 + 1; #include < ...
- Memcached 及 Redis 架构分析和比较
Memcached和Redis作为两种Inmemory的key-value数据库,在设计和思想方面有着很多共通的地方,功能和应用方面在很多场合下(作为分布式缓存服务器使用等) 也很相似,在这里把两者放 ...
- nginx日志切割
脚本完成后将其存入 Nginx 安装目录的 sbin 中,取名为 cut-log.sh,之后使用 crontab -e 新增一个定时任务,在其中增加执行这个脚本: #!/bin/bash ## 零点执 ...
- http://bootswatch.com/
http://bootswatch.com/ http://v3.bootcss.com/examples/dashboard/
- Windows8 10设置程序为 系统默认浏览器
从win8 开始,MS修改了文件和协议的关联方式,普通的注册表修改是无效的. 必须使用组策略(group policy )对象GP才行. http://blogs.technet.com/b/mrml ...
- WP8.1 Study3:WP8.1中Animation应用
WP8.1上的Animation动画的API和WIN8/WIN8.1上的差不多,网上可以找到很多资料,同时可以去MSDN看官方文档. 下面是我参考一些资料,写出来的例子,希望以后有用. xaml代码如 ...