JS数据结构与算法--双向链表
双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示:
双向链表结构代码如下:
class Node {
constructor(element) {
this.element = element;
this.prev = null;
this.next = null;
}
} class DoubledLinskLIst {
constructor() {
this.length = 0;
this.head = null;
this.tail = null;
} append(element) {
let node = new Node(element);
if (this.getHead() === null) { //空链表的情况
this.head = node;
this.tail = node; //如果是空链表,插入一个元素,它的head和tail都指向node
}
else {
let current = this.getTail();
current.next = node;
node.prev = current;
this.tail = node;
}
this.length++;
} insert(position, element) { if (position >= 0 && position <= this.size()) {
let current = this.getHead(),
previous,
index = 0,
node = new Node(element);
if (position === 0) {
if (!this.getHead()) { //空链表
this.head = node;
this.tail = node;
}
else {
this.head = node;
node.next = current;
current.prev = node;
} }
else if (position === this.size()) {
current = this.getTail();
this.tail = node;
node.prev = current;
current.next = node;
}
else { while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = node;
node.prev = previous;
node.next = current;
current.prev = node; }
this.length++;
return true;
}
else {
return false;
}
} removeAt(position) {
if (position >= 0 && position < this.size()) {
let current = this.getHead(), index = 0, previous;
if (position === 0) {
this.head = current.next;
if (this.size() === 1) { //链表只有一个数据
this.tail = null;
}
else {
current.next.prev = null;
} }
else if (position === this.size() - 1) {
current = this.getTail();
this.tail = current.prev;
current.prev.next = null;
}
else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous; }
this.length--;
return current.element;
}
else {
return null;
}
} indexOf(element) {
let current = this.getHead(), index = 0;
while (current) {
if (current.element === element) {
return index;
}
index++;
current = current.next;
}
return -1;
} remove(element) {
let position = this.indexOf(element);
return this.removeAt(position);
} getHead() {
return this.head;
} getTail() {
return this.tail;
} size() {
return this.length;
} isEmpty() {
return this.length === 0;
} toString() { let current = this.getHead(),
string = ''; while (current) {
string += current.element + (current.next ? ', ' : '');
current = current.next;
}
return string; } print() {
console.log(this.toString());
}
}
参考:《JavaScript数据结构与算法--第二版》
JS数据结构与算法--双向链表的更多相关文章
- js数据结构与算法--双向链表的实现
双向链表也叫双链表,是链表的一种,它的每个数据节点中都有两个指针,分别指向直接后继和直接前驱.所以,双向链表中的任意一个节点开始,都可以很方便的访问它的前驱节点和后继节点. 双向链表的实现 linke ...
- JS数据结构与算法——栈
JS数据结构与算法--栈 1.栈结构概念 栈(Stack)是一种先进后出(LIFO Last in First out)的线性表,先进栈的将会比后进栈的先出栈. 栈的限制是仅允许在一端进行插入和删除运 ...
- JS数据结构与算法-概述
JS数据结构与算法概述 数据结构: 计算机存储, 组织数据的方式, 就像锅碗瓢盆 算法: 一系列解决问题的清晰指令, 就像食谱 两者关系: 程序 = 数据结构 + 算法 邂逅数据结构与算法 什么是数据 ...
- JS数据结构及算法(二) 队列
队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...
- JS数据结构及算法(一) 堆栈
最近在看<学习JavaScript数据结构与算法>这本书,感觉自己又涨知识了 哈哈... 现在将自己看的做个总结,也是巩固理解. 栈:先进后出,新添加和待删除的元素都保存在栈顶.可以用数组 ...
- js数据结构与算法--单链表的实现与应用思考
链表是动态的数据结构,它的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 现实中,有一些链表的例子. 第一个就是寻宝的游戏.你有一条线索,这条线索是指向寻找下一条线 ...
- JS数据结构与算法 - 剑指offer二叉树算法题汇总
❗❗ 必看经验 在博主刷题期间,基本上是碰到一道二叉树就不会碰到一道就不会,有时候一个下午都在搞一道题,看别人解题思路就算能看懂,自己写就呵呵了.一气之下不刷了,改而先去把二叉树的基础算法给搞搞懂,然 ...
- js数据结构与算法存储结构
数据结构(程序设计=数据结构+算法) 数据结构就是关系,没错,就是数据元素相互之间存在的一种或多种特定关系的集合. 传统上,我们把数据结构分为逻辑结构和物理结构. 逻辑结构:是指数据对象中数据元素之间 ...
- JS数据结构与算法-队列结构
队列结构 一.认识队列 受限的线性结构: 我们已经学习了一种受限的线性结构:栈结构. 并且已经知道这种受限的数据结构对于解决某些特定问题,会有特别的 效果. 下面,我们再来学习另外一个受限的数据结构: ...
随机推荐
- Python基础:模块化来搭项目
简单模块化 import 最好在最顶端 sys.path.append("..")表示把当前程序所在位置向上提了一级 在python3规范中,__init__.py并不是必须的. ...
- 上传、裁剪图片-----Jcrop图片裁剪插件
Jcrop文档:http://code.ciaoca.com/jquery/jcrop/C#裁剪:http://www.cnblogs.com/xyang/archive/2013/02/25/293 ...
- springboot集成log4j
需求: 1.springboot集成log4j 2.mybatis 打印 sql 实现: pom.xml <dependency> <groupId>org.springfra ...
- java Integer
Java 中的数据类型分为基本数据类型和引用数据类型 int是基本数据类型,Integer是引用数据类型: Ingeter是int的包装类,int的初值为0,Ingeter的初值为null. 初始化 ...
- (转) Linux 下的dd命令使用详解(摘录)
使用dd命令克隆整个系统------http://www.cnblogs.com/jikexianfeng/p/6103504.html 原文:https://www.cnblogs.com/jike ...
- 看完这篇文章,我奶奶都懂了https的原理
本文在个人技术博客同步发布,详情可猛戳 亦可扫描屏幕右方二维码关注个人公众号 Http存在的问题 上过网的朋友都知道,网络是非常不安全的.尤其是公共场所很多免费的wifi,或许只是攻击者的一个诱饵 ...
- java join 方法的使用
在很多情况下,主线程创建并启动子线程,如果子线程中要进行大量的耗时运算,主线程往往将早于子线程结束之前结束.这时,如果主线程想等待子线程执行完成之后再结束,比如子线程处理一个数据,主线程要取得这个数据 ...
- scrollHelper
(function ($) { var mouseScroll = function (e) { try { var origEvent = e.originalEvent; origEvent.pr ...
- 洛谷-P3927 SAC E#1 - 一道中档题 Factorial
原址 题目背景 数据已修改 SOL君(炉石主播)和SOL菌(完美信息教室讲师)是好朋友. 题目描述 SOL君很喜欢阶乘.而SOL菌很喜欢研究进制. 这一天,SOL君跟SOL菌炫技,随口算出了n的阶乘. ...
- SPRING代理模式
1.静态代理 主题对象:Student public interface Student { public String add(); } 目标对象:RealStudent public class ...