单链表地址:点我

一、循环链表

  节点的next指向下一个节点,节点的prev指向上一个节点

function loopList() {
let length = 0,
head = null,
tail = null
this.append = (data) => {
let node = new Node(data),
current
if(head === null) {
head = node
tail = node
}else {
current = head
while(current.next) {
current = current.next
}
current.next = node
tail = node
}
length ++
return true
}
this.insert = (position, data) => {
if(position > -1 && position <= length) {
let node = new Node(data),
current = head,
previous
if(position === 0) {
if (!head) {
head = node
tail = node
}else {
node.next = current
current.prev = node
head = node
}
}
else if(position === length) {
current = tail
current.next = node
node.prev = current
tail = node
}
else {
while(index ++ < position) {
previous = current
current = current.next
}
previous.next = node
node.prev = previous
node.next = current
current.prev = node
}
length ++
return true
}else {
return false
}
}
this.removePos = function (position) {
//检查是否越界
if (position > -1 && position < length) {
var current = head,
previous,
index = 0 if (position === 0) { //移除第一项
head = current.next
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
}
//将previous与current的下一项链接起来,跳过current,从而移除它
previous.next = current.next
current.next.prev = previous
}
length --
return true
}else {
return false
}
}
this.removeData = (data) => {
if(head === null) {
return false
}
else {
let current = head,
previous,
index = 0
if (length === 1 ) {
if (current.data !== data) {
return false
}else {
head = null
tail = null
length --
return true
}
} while (index ++ < length && current.data !== data) {
previous = current
current = current.next
}
if(index === length) {
current = tail
tail = current.prev
tail.next = null
length --
return true
}
if(index > length) {
return false
}else {
previous.next = current.next
current.next.prev = previous
length --
return true
}
}
}
this.toString = () => {
let resultStr = "",
current,
index = length
if(head === null) {
return ""
}else {
current = head
while(index -- > 0) {
resultStr += "," + current.data
current = current.next
}
return resultStr.slice(1)
}
}
} function Node(data) {
this.data = data
this.next = null
this.prev = null
}

  

JavaScript实现循环链表的更多相关文章

  1. javascript中使用循环链表实现约瑟夫环问题

    1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...

  2. javascript实现数据结构与算法系列:循环链表与双向链表

    循环链表(circular linked list) 是另一种形式的链式存储结构.它的特点是表中最后一个结点的指针域指向头结点,整个表形成一个环. 循环链表的操作和线性链表基本一致,仅有细微差别. w ...

  3. JavaScript数据结构-9.循环链表

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. javascript LinkedList js 双向循环链表 Circular Linked List

    javascript LinkedList: function Node(elem, prev, next) { this.elem = elem; this.prev = prev ? prev : ...

  5. 学习javascript数据结构(二)——链表

    前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...

  6. 数据结构与算法之链表-javascript实现

    链表的定义: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点 ...

  7. 数据结构与算法JavaScript (三) 链表

    我们可以看到在javascript概念中的队列与栈都是一种特殊的线性表的结构,也是一种比较简单的基于数组的顺序存储结构.由于javascript的解释器针对数组都做了直接的优化,不会存在在很多编程语言 ...

  8. javascript数据结构和算法

    一.栈 javascript实现栈的数据结构(借助javascript数组原生的方法即可) //使用javascript来实现栈的数据结构 var Stack={ //不需要外界传参进行初始化,完全可 ...

  9. JavaScript中的算法之美——栈、队列、表

    序 最近花了比较多的时间来学习前端的知识,在这个期间也看到了很多的优秀的文章,其中Aaron可能在这个算法方面算是我的启蒙,在此衷心感谢Aaron的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...

随机推荐

  1. IdentityServer4 记录

    IdentityServer4 文档 https://www.cnblogs.com/edisonchou/p/identityserver4_foundation_and_quickstart_01 ...

  2. idea整合 spring boot jsp mybatis

    spring  boot  开发起来确实要简单许多 ,spring boot  包含了 spring mvc ;内置tomcat   ;启动只需要主方法即可 1.使用idea新建一个spring bo ...

  3. 读取InputStream 中的内容

      读取InputStream 中的内容 ]) { , len); //把读取到的内容写到输出流中 } //<4> 把字节数组转换为字符串 String content = baos.to ...

  4. python 进程间通信(上)

    一  使用queue来实现进程间的内存共享 #_*_coding:utf-8_*_ from multiprocessing import Process,Queue import os,time d ...

  5. linux中python3安装和使用

    python安装 下载python安装包和依赖环境 #自由选择python3源码包的版本https://www.python.org/ftp/python/https://www.python.org ...

  6. dbconfig.properties

    jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/数据库名 jdbc.username=root jdbc. ...

  7. linux安装zookeeper

    安装环境:Linux:centos6.4Jdk:1.7以上版本 Zookeeper是java开发的可以运行在windows.linux环境.需要先安装jdk.安装步骤:第一步:安装jdk第二步:把zo ...

  8. 数据结构day1:排序

    1,冒泡排序算法的python实现 def bubble_sort(alist): pass count = len(alist)-1 for index in range(count,0,-1): ...

  9. hue,kylin,ambari

    apache-kylin https://ambari.apache.org/ https://www.jianshu.com/p/c49c61b654da docker pull sequencei ...

  10. 6. Scala面向对象编程(基础部分)

    6.1 基本介绍 6.1.1 Scala语言是面向对象的 1) Java时面向对象的编程语言,由于历史原因,Java中海存在着非面向对象的内容:基本类型,null,静态方法等 2) Scala语言来自 ...