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的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...
随机推荐
- sort实现ip排序
说明: 工具:sort linux自带 ”-t” : 表示以那个字符做分割 “-k” :和-t结合使用,表示取那一段为关键字进行排序,后面跟数据,1…n,表示取第几段,也可 ...
- elk安装时最常见的报错
1.在启动kibana的时候报一下错误 max file descriptors [4096] for elasticsearch process likely too low, increase t ...
- 自学华为IoT物联网_08 IoT连接管理平台介绍
点击返回自学华为IoT物流网 自学华为IoT物联网_08 IoT连接管理平台介绍 一.IoT连接管理平台的由来 1.1 物联网产业发展面临的挑战 新业务上线周期长,应用碎片化,开发周期长,场频上市慢 ...
- 水晶报表,快速报表,rdlc报表
感觉自己脑子里只剩下报表了,o(╥﹏╥)o.因为最近新换了公司,业务上有需要报表打印,水晶报表,快速报表,rdlc报表这三种以后可能都会用到.所以在没了解好业务流程,熟悉代码之前,就是看看这三种报表怎 ...
- Vim使用心得
马上就要联赛啦. 学习一下vim 这是一个vim文档 http://vimcdoc.sourceforge.net/doc/ 这是一个优秀的vimrc配置 http://www.cnblogs.com ...
- PHPstorm远程连接侧边栏怎么打开,远程数据库侧边栏怎么打开
- SD卡
一.SD卡接口 SD 卡的接口可以支持两种操作模式:主机系统可以选择以上其中任一模式, SD 卡模式允许 4 线的高速数据传输. SPI 模式允许简单通用的 SPI 通道接口, 这种模式相对于 SD ...
- vue_简介_渐进式 js 框架_内置指令_自定义指令_自定义插件
vue 尤雨溪 华裔 Google 工程师 遵循 MVVM 模式 编码简洁,体积小,运行效率高,适合 移动 / PC 端 开发 动态构建用户界面: 异步获取后台数据,展现到页面 渐进式 js 框架 渐 ...
- [LeetCode] Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- poj1207 3n+1 problem
The 3n + 1 problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 60496 Accepted: 19 ...