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数据结构与算法-队列结构
队列结构 一.认识队列 受限的线性结构: 我们已经学习了一种受限的线性结构:栈结构. 并且已经知道这种受限的数据结构对于解决某些特定问题,会有特别的 效果. 下面,我们再来学习另外一个受限的数据结构: ...
随机推荐
- Luogu P3941 入阵曲【前缀和】By cellur925
题目传送门 题目大意:给你一个\(n\)*\(m\)的矩阵,每个位置都有一个数,求有多少不同的子矩阵使得矩阵内所有数的和是\(k\)的倍数. 数据范围给的非常友好233,期望得到的暴力分:75分.前1 ...
- C 语言实例 - 求两数最小公倍数
C 语言实例 - 求两数最小公倍数 用户输入两个数,其这两个数的最小公倍数. 实例 - 使用 while 和 if #include <stdio.h> int main() { int ...
- net core (上)
net core (上) 本文是基于Windows10的. 下载地址: https://code.visualstudio.com/ insider 版下载地址: https://code.visua ...
- 牛客网Java刷题知识点之抽象类与接口
不多说,直接上干货! 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于它们的存在才赋予java强大的面向对象的能力. ...
- Adobe CC Family (CC 2015) 大师版
Adobe CC Family (CC 2015) 大师版 v5.6#2 ###请彻底卸载旧版后再安装本版! 更新 Adobe Digital Publishing CC 2016.1更新 Adobe ...
- memcache和iptables开启11211端口
linux下安装完memcached后,netstat -ant | grep LISTEN 看到memcache用的11211端口已在监听状态,但建立php文件连接测试发现没有输出结果,iptabl ...
- HangFire 定时任务
https://www.cnblogs.com/ecin/p/6201262.html#%E5%9F%BA%E4%BA%8E%E9%98%9F%E5%88%97%E7%9A%84%E4%BB%BB%E ...
- 发现了一个entity framework的BUG
小弟学浅才疏可能是小题大做,但遇上了并且让我麻烦了一阵,就值得记下来 BUG的过程就是我在建立实体模型的时候 命名了一个叫system的实体模型 导致了所有生成类中 引用using system失败
- java学习笔记(3)——对象与类(日期)
变量.类型.赋值.运算符等等: https://blog.csdn.net/common77zwq/article/details/81988676 1.概念: 面向对象程序设计OOP.类class. ...
- I/O操作总结(一)
所谓IO,也就是Input与Output的缩写.在java中,IO涉及的范围比较大,这里主要讨论针对文件内容的读写 其他知识点将放置后续章节(我想,文章太长了,谁都没耐心翻到最后) 对于文件内容的操作 ...