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的付出和奉献,同时自己也会坚定的走前人这种无私奉献的分 ...
随机推荐
- C3_note
- vscode断点调试工程化服务端文件
一.创建express应用我们使用express-generator创建一个新的express应用.1.全局安装express-generator // 安装 sudo npm install exp ...
- 解读vscode断点调试配置文件【待续】
一.参考链接 https://code.visualstudio.com/Docs/editor/debugging https://code.visualstudio.com/docs/nodejs ...
- 【转】C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。
C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子. 标签: c#objectnewlineexceptionbytestring 2010-05-17 01:10 117109人阅读 ...
- svn打分支和合并操作
1.svn打分支 到trunk里,选择Branch/tag.... 填写分支版本路径 到branch里svn up 一下,就有1.4.0分支了 2.svn合并 到trunk里,选择Merge.. 选择 ...
- 转 MYSQL InnoDB Record, Gap, and Next-Key Locks
http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html InnoDB has several types of re ...
- 使用JS获取input值
获取input值,设置input值 可以使用 $(".class") $("#id") $("input[name='name']") re ...
- 剑指offer——python【第4题】重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- 问题:CGI返回给前端的汉字数据是乱码(已解决)
今天,我在写CGI程序,把后台数据返回到前台页面上,出现乱码, 不好看,所以,我在Content-type:application后面加了字符集的设置(字符集和前端一样), fprintf(stdou ...
- c#中string的一些基本用法
.string的Split方法的使用 这个例子就是通过制定的符号来将词组分开,Splite(分割的字符,分割的份数) using System; using System.Collections; p ...