JavaScript数据结构与算法-链表练习
链表的实现
一. 单向链表
// Node类
function Node (element) {
this.element = element;
this.next = null;
}
// LinkedList类
function LList () {
this.head = new Node('head');
this.find = find;
this.insert = insert;
this.findPrevious = findPrevious;
this.remove = remove;
this.display = display;
}
// 查找
function find (item) {
let currNode = this.head;
while (currNode.element !== item) {
currNode = currNode.next;
}
return currNode;
}
// 插入
function insert (newElement, item) {
let newNode = new Node(newElement);
let current = this.find(item);
newNode.next = current.next;
current.next = newNode;
}
// 显示
function display () {
let currNode = this.head;
while (currNode.next !== null) {
currNode = currNode.next;
console.log(currNode.element);
}
}
// 检查下一个节点
function findPrevious (item) {
let currNode = this.head;
while (currNode.next !== null && currNode.next.element !== item) {
currNode = currNode.next;
}
return currNode;
}
// 删除
function remove (item) {
let prevNode = this.findPrevious(item);
if (prevNode.next !== null) {
prevNode.next = prevNode.next.next;
}
}
二. 双向链表
function Node (element) {
this.element = element;
this.next = null;
this.previous = null;
}
function DList () {
this.head = new Node('head');
this.find = find;
this.insert = insert;
this.display = display;
this.remove = remove;
this.findLast = findLast;
this.dispReverse = dispReverse;
}
function dispReverse () {
let currNode = this.head;
currNode = this.findLast();
while (currNode !== null && currNode.element !== 'head') {
console.log(currNode.element);
currNode = currNode.previous;
}
}
function findLast () {
let currNode = this.head;
while (currNode.next !== null) {
currNode = currNode.next;
}
return currNode;
}
function remove (item) {
let currNode = this.find(item);
if (currNode.next !== null) {
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
}
function display () {
let currNode = this.head;
while (currNode.next !== null) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function find (item) {
let currNode = this.head;
while (currNode.element !== item) {
currNode = currNode.next;
}
return currNode;
}
function insert (newElement, item) {
let newNode = new Node(newElement);
let currNode = this.find(item);
newNode.next = currNode.next;
newNode.previous = currNode;
currNode.next = newNode;
}
三. 循环链表
// Node类
function Node (element) {
this.element = element;
this.next = null;
}
// LinkedList类
function CList () {
this.head = new Node('head');
// 修改
this.head.next = this.head;
this.find = find;
this.insert = insert;
this.findPrevious = findPrevious;
this.remove = remove;
this.display = display;
}
// 其他
// ...
练习
一. 实现advance(n)方法,使当前节点向前移动n个节点。
// 向前移动一位
DList.prototype.goPrevious = function (thisNode) {
let node1, node2, node3, node4;
if (thisNode.previous.element !== 'head') {
node1 = thisNode.previous.previous;
node2 = thisNode.previous;
node3 = thisNode;
node4 = thisNode.next;
// 位置调整
node1.next = node3;
node3.previous = node1;
node3.next = node2;
node2.previous = node3;
node2.next = node4;
node4.previous = node2;
}
};
DList.prototype.advance = function (n, item) {
let currNode = this.find(item);
while (n--) {
this.goPrevious(currNode);
}
};
// 示例
let names = new DList();
names.insert('Mazey', 'head');
names.insert('Cherrie', 'Mazey');
names.insert('John', 'Cherrie');
names.insert('Luna', 'John');
names.insert('Ada', 'Luna');
names.display();
console.log('---');
names.advance(2, 'Luna');
names.display(); // Mazey, Luna, Cherrie, John, Ada
二. 实现back(n)方法,使当前节点向后移动n个节点。
// 向前移动一位
DList.prototype.goNext = function (thisNode) {
let node1, node2, node3, node4;
if (thisNode.next.element !== null) {
node1 = thisNode.previous;
node2 = thisNode;
node3 = thisNode.next;
node4 = thisNode.next.next;
// 位置调整
node1.next = node3;
node3.previous = node1;
node3.next = node2;
node2.previous = node3;
node2.next = node4;
node4.previous = node2;
}
};
DList.prototype.back = function (n, item) {
let currNode = this.find(item);
while (n--) {
this.goNext(currNode);
}
};
// 示例
let names = new DList();
names.insert('Mazey', 'head');
names.insert('Cherrie', 'Mazey');
names.insert('John', 'Cherrie');
names.insert('Luna', 'John');
names.insert('Ada', 'Luna');
names.display();
console.log('---');
names.back(2, 'Cherrie');
names.display(); // Mazey, John, Luna, Cherrie, Ada
JavaScript数据结构与算法-链表练习的更多相关文章
- javascript数据结构与算法--链表
链表与数组的区别? 1. 定义: 数组又叫做顺序表,顺序表是在内存中开辟一段连续的空间来存储数据,数组可以处理一组数据类型相同的数据,但不允许动态定义数组的大小,即在使用数组之前必须确定数组的大小. ...
- 为什么我要放弃javaScript数据结构与算法(第五章)—— 链表
这一章你将会学会如何实现和使用链表这种动态的数据结构,这意味着我们可以从中任意添加或移除项,它会按需进行扩张. 本章内容 链表数据结构 向链表添加元素 从链表移除元素 使用 LinkedList 类 ...
- 重读《学习JavaScript数据结构与算法-第三版》- 第6章 链表(一)
定场诗 伤情最是晚凉天,憔悴厮人不堪言: 邀酒摧肠三杯醉.寻香惊梦五更寒. 钗头凤斜卿有泪,荼蘼花了我无缘: 小楼寂寞新雨月.也难如钩也难圆. 前言 本章为重读<学习JavaScript数据结构 ...
- JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)
前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...
- 为什么我要放弃javaScript数据结构与算法(第九章)—— 图
本章中,将学习另外一种非线性数据结构--图.这是学习的最后一种数据结构,后面将学习排序和搜索算法. 第九章 图 图的相关术语 图是网络结构的抽象模型.图是一组由边连接的节点(或顶点).学习图是重要的, ...
- 为什么我要放弃javaScript数据结构与算法(第八章)—— 树
之前介绍了一些顺序数据结构,介绍的第一个非顺序数据结构是散列表.本章才会学习另一种非顺序数据结构--树,它对于存储需要快速寻找的数据非常有用. 本章内容 树的相关术语 创建树数据结构 树的遍历 添加和 ...
- 为什么我要放弃javaScript数据结构与算法(第七章)—— 字典和散列表
本章学习使用字典和散列表来存储唯一值(不重复的值)的数据结构. 集合.字典和散列表可以存储不重复的值.在集合中,我们感兴趣的是每个值本身,并把它作为主要元素.而字典和散列表中都是用 [键,值]的形式来 ...
- 为什么我要放弃javaScript数据结构与算法(第六章)—— 集合
前面已经学习了数组(列表).栈.队列和链表等顺序数据结构.这一章,我们要学习集合,这是一种不允许值重复的顺序数据结构. 本章可以学习到,如何添加和移除值,如何搜索值是否存在,也可以学习如何进行并集.交 ...
- 为什么我要放弃javaScript数据结构与算法(第四章)—— 队列
有两种结构类似于数组,但在添加和删除元素时更加可控,它们就是栈和队列. 第四章 队列 队列数据结构 队列是遵循FIFO(First In First Out,先进先出,也称为先来先服务)原则的一组有序 ...
随机推荐
- (转)JavaScript: in, hasOwnProperty, delete, for/in
in 运算符 判断对象是否拥有某一属性只要对象拥有该属性,就会返回true,否则false var point = { x:1, y:1 };alert( 'x' in point ); //tru ...
- Python课程之元组
元组(Tuple) 一.定义: 与列表(list)不同的是,元组不支持修改,但是若元组中的元素本身是可变对象,如列表,则可以修改.元素之间用逗号隔开,并且元素的类型可以任意. 二.操作: 1.创建:直 ...
- tcp/ip --- IP路由选择及子网寻址
IP路由选择 当一个IP数据包准备好了的时候,IP数据包(或者说是路由器)是如何将数据包送到目的地的呢?它是怎么选择一个合适的路径来"送货"的呢? 最特殊的情况是目的主机和主机直连 ...
- xilinx平台DDR3设计教程之仿真篇_中文版教程
https://wenku.baidu.com/view/ac32c8bcf705cc1754270923.html https://wenku.baidu.com/view/1d665697f185 ...
- cookie转coontoin
/// <summary> /// 一个到多个Cookie的字符串添加到CookieCollection集合中[isGood代码] /// </summary> /// < ...
- UVA 11885 - Number of Battlefields(斐波那契)
11885 - Number of Battlefields 题意:给周长.求能围成的战场数目.不包含矩形. 思路:详细的递推没递推出来,可是看了网上一个规律,假设包含矩形的答案应该是斐波那契数列(可 ...
- std::copy ( myvector.begin(), myvector.end(), out_it )
在实际生产环境中,不能进行调试,所以程序通常需要编译一个DEBUG版本来辅助我们找出问题所在,编译这样的DEBUG版本最常用的手段就是在关键处输出我们关心一些变量的值到屏幕. 如果输出的简单的变量值, ...
- SQL注入-数据库判断
0x01.sql注入 sql注入是在系统开发的过程中程序员编程不规范,我们可以通过把SQL语句插入到WEB表单中进行查询字符串,最终达成欺骗服务器执行恶意的SQL命令.对于现在的网站SQL注入越来越严 ...
- Spring MVC属性方法名称解析器
以下示例显示如何使用Spring Web MVC框架来实现多动作控制器的属性方法名称解析器. MultiActionController类可在单个控制器中分别映射多个URL到对应的方法. 所下所示配置 ...
- Spring MVC复选框
以下示例显示如何在使用Spring Web MVC框架的表单中使用复选框(Checkbox).首先使用Eclipse IDE来创建一个WEB工程,并按照以下步骤使用Spring Web Framewo ...