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的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...
随机推荐
- 转:mongoDB 修改 ulimit
转自:http://blog.yucanlin.cn/2015/03/23/mongodb-%E4%BF%AE%E6%94%B9-ulimit/ mongoDB 修改 ulimit 一切都源于mong ...
- Service的绑定过程
--摘自<Android进阶解密> 第一步:ContextImpl到AMS的调用过程 第二步:Service的绑定过程 1)几个与Service相关的对象类型 * ServiceRecor ...
- Mysql的学习笔记03
---恢复内容开始--- Mysql 的视图 1 view 在查询中,我们经常把查询结果当成临时表来看, view 是什么? View 可以看成一张虚拟的表,是表通过某种运算得到的有一个投影. 2 ...
- [Error]Python虚拟环境报错 OSError: setuptools pip wheel failed with error code 2
mkvirtualenv py35 python新建虚拟环境报错,setuptools pip wheel failed with error code 2 刚好昨天在CentOS安装的时候也总是报s ...
- Django——发送邮件
Django--发送邮件 在web应用中,服务器对客户发送邮件来通知用户一些信息,可以使用邮件来实现. Django中提供了邮件接口,使我们可以快捷的建设一个邮件发送系统. 以下是一个简单实例: se ...
- Android源代码编译过程及指令
编译Android源代码分为两种情况: 1. 完整编译源码: ./mk_aliphone.sh --> 完整编译脚本 --> 6735 输入对应的编号 --> userdebug ...
- 如何运行后台Service?
Unless you specify otherwise, most of the operations you do in an app run in the foreground on a spe ...
- buffer格式的转换
---恢复内容开始--- 定义好一个buffer 例如: var buf = new Buffer(''nihaoya); 通过str转成base64的字符 var str =buf.toString ...
- mac上adb command not found
第一种报错(使用的自带mac命令行) bash: adb: command not found 1.vim ~/.bash_profile ,如果.bash_profile不存在,先touch ~/. ...
- json 函数
一, json序列化和反序列化 JSON.stringify( ) —— 将对象序列化为JSON字符串 JSON.parse( ) —— 将JSON数据解析为Javascript对象 二,判断json ...