JavaScript实现循环链表
单链表地址:点我
一、循环链表
节点的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实现循环链表的更多相关文章
- javascript中使用循环链表实现约瑟夫环问题
1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...
- javascript实现数据结构与算法系列:循环链表与双向链表
循环链表(circular linked list) 是另一种形式的链式存储结构.它的特点是表中最后一个结点的指针域指向头结点,整个表形成一个环. 循环链表的操作和线性链表基本一致,仅有细微差别. w ...
- JavaScript数据结构-9.循环链表
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- javascript LinkedList js 双向循环链表 Circular Linked List
javascript LinkedList: function Node(elem, prev, next) { this.elem = elem; this.prev = prev ? prev : ...
- 学习javascript数据结构(二)——链表
前言 人生总是直向前行走,从不留下什么. 原文地址:学习javascript数据结构(二)--链表 博主博客地址:Damonare的个人博客 正文 链表简介 上一篇博客-学习javascript数据结 ...
- 数据结构与算法之链表-javascript实现
链表的定义: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点 ...
- 数据结构与算法JavaScript (三) 链表
我们可以看到在javascript概念中的队列与栈都是一种特殊的线性表的结构,也是一种比较简单的基于数组的顺序存储结构.由于javascript的解释器针对数组都做了直接的优化,不会存在在很多编程语言 ...
- javascript数据结构和算法
一.栈 javascript实现栈的数据结构(借助javascript数组原生的方法即可) //使用javascript来实现栈的数据结构 var Stack={ //不需要外界传参进行初始化,完全可 ...
- JavaScript中的算法之美——栈、队列、表
序 最近花了比较多的时间来学习前端的知识,在这个期间也看到了很多的优秀的文章,其中Aaron可能在这个算法方面算是我的启蒙,在此衷心感谢Aaron的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...
随机推荐
- ssh报错 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
今天登陆远程主机的时候,出现如下的报错信息 ssh 10.0.0.1 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WAR ...
- 必须知道的Linux内核常识详解
一.内核功能.内核发行版 1.到底什么是操作系统 (1)linux.windows.android.ucos就是操作系统: (2)操作系统本质上是一个程序,由很多个源文件构成,需要编译连接成操作系统程 ...
- 程序猿最浪漫的表白,肯定会得到你的她——Jason niu 原文来自GitHub,本人已部分修改
程序猿最浪漫的表白,肯定会得到你的她——Jason niu 原文来自GitHub,主页本人已部分修改,感谢程序猿大神wuxia2001和hackerzhou的开源,感谢这两位大神! 视频结果展示 ...
- Service的绑定过程
--摘自<Android进阶解密> 第一步:ContextImpl到AMS的调用过程 第二步:Service的绑定过程 1)几个与Service相关的对象类型 * ServiceRecor ...
- Metasploit学习记录---Nessus安装部署
1.Nessus介绍 nessus是目前世界上最为流行的漏洞扫描器之一.她提供完整的电脑漏洞扫描服务,并随时更新其漏洞数据库.Nessus不同于传统的漏洞扫描软件,可同时在本机或远端上遥控,进行系统的 ...
- 2019-2-14SQLserver中拼音查询数据
SQLserver中获取文字的全拼音: CREATE function [dbo].[f_GetPinyin](@words nvarchar()) returns varchar() as begi ...
- SDOI2018:荣誉称号
题解: https://files.cnblogs.com/files/clrs97/title-solution.pdf Code: #include<cstdio> #include& ...
- linux 文件属性(转)
1. 文件类型 - 普通文件 d 目录文件 l 链接文件 b 块设备文件 c 字符型设备文件 s socket文件 p 管道类型文件 块设备文件主要是指慢速设备,比如hd硬盘,数据主要是分块存储,所 ...
- NOIP-金币
题目描述 国王将金币作为工资,发放给忠诚的骑士.第一天,骑士收到一枚金币:之后两天(第二天和第三天),每天收到两枚金币:之后三天(第四.五.六天),每天收到三枚金币:之后四天(第七.八.九.十天),每 ...
- [LeetCode] Quad Tree Intersection 四叉树相交
A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight, ...