双向链表、双向循环链表的JS实现
关于链表简介、单链表、单向循环链表、JS中的使用以及扩充方法: 单链表、循环链表的JS实现
关于四种链表的完整封装: https://github.com/zhuwq585/Data-Structure-in-JavaScript/blob/master/LinkedList.js
双向链表:单向链表只能向着一个方向遍历链表节点,而在节点指针域中增加了前向指针的双向链表,则可以向着两个方向遍历节点。这使得双向链表也可以在任何一个节点遍历整个链表。
function DoublyLinkedList() {
var Node = function(element) {
this.element = element;
this.next = null;
this.prev = null;
}; var length = 0,
head = null,
tail = null; this.append = function(element){
var node = Node(element),
current,
previous; if(!head){
head = node;
tail = node;
}else{
current = head;
while(current){
previous = current;
current = current.next;
} node.next = current;
current.prev = node;
previous.next = node;
node.prev = previous;
} length++;
return true;
} this.insert = function(position,element){
if(position > -1 && position < length){
var node = new Node(element),
current = head,
previous,
index = 0; if(position === 0){ if(!head){
head = node;
tail = node;
}else{
node.next = current;
current.prev = node;
head = node;
} }else if (position === length -1){
current = tail;
current.next = node;
node.prev = current;
}else {
while(index++ < position){
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
current.prev = node;
node.prev = previous;
} length++;
return true;
}else{
return false;
}
}; this.removeAt = function(position){
if(position > -1 && position < length){
var current = head,
index = 0,
previous; 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.next = current.next;
current.next.prev = previous;
};
length-- ; return current.element;
}else{
return false;
}
}; this.remove = function(element){
var current = head,
previous; if(current.element === element){
head = current.next;
}
previous = current;
current = current.next; while(current){
if (current.element = element) {
previous.next = current.next;
current.next.prev = previous;
}else{
previous = current;
current = current.next;
}
}
return false;
}; this.remove = function(){
if (length === 0) {
return false;
}; var current = head,
previous; if(length === 1){
head = null;
tail = null;
length--;
return current.element;
} while(current){
previous = current;
current = current.next;
} previous.next = null;
length--;
return current.element;
}; this.indexOf = function(element){
var current = head,
index = 0; while(current && index++ < length){
if (current.element === element) {
return index;
};
current = current.next;
} return false;
}; this.isEmpty = function(){
return length === 0;
}; this.size = function(){
return length;
}; this.toString = function(){
var current = head,
string = ''; while(current){
string += current.element;
current = current.next;
}
return string;
}; this.getHead = function(){
return head;
}; this.getTail = function(){
return tail;
};
}
双向循环链表:将双向链表的头尾指针相连,就构成了双向循环链表。这种链表从任意一个节点都可以同时向两个方向进行节点遍历,查询节点的速度也是最快的。
/*双向循环链表*/
function DoublyCircularLinkedList(){
var Node = function(element){
this.element = element;
this.next = null;
this.prev = null;
}; var length = 0,
head = null,
tail = null; this.append = function(element){
var node = new Node(element),
current,
previous; if (!head) {
head = node;
tail = node;
head.prev = tail;
tail.next = head;
}else{
current = head; while(current.next !== head){
previous = current;
current = current.next;
} current.next = node;
node.next = head;
node.prev = current;
}; length++;
return true;
}; this.insert = function(position, element){
if(position >= 0 && position <= length){
var node = new Node(element),
index = 0,
current = head,
previous; if(position === 0){ if(!head){ node.next = node;
node.tail = node;
head = node;
tail = node; }else{ current.prev = node;
node.next = current;
head = node;
node.prev = tail; } }else if(position === length){
current = tail; current.next = node;
node.prev = current;
tail = node;
node.next = head;
}else{ while(index++ < position){
previous = current;
current = current.next;
} current.prev = node;
node.next = current;
previous.next = node;
node.prev = previous; } length++;
return true;
}else{
return false;
}
}; this.removeAt = function(position){
if(position > -1 && position < length){ var current = head,
index = 0,
previous; if(position === 0){ current.next.previous = tail;
head = current.next; }else if(position === length - 1){ current = tail; current.prev.next = head;
head.prev = current.prev;
tail = current.prev;
}else{ while(index++ < position){
previous = current;
current = current.next;
} previous.next = current.next;
current.next.prev = previous; } length--;
return true;
}else{
return false;
}
}; this.remove = function(element){
var current = head,
previous,
indexCheck = 0; while(current && indexCheck < length){
if(current.element === element){
if(indexCheck === 0){
current.next.prev = tail;
head = current.next;
}else{
current.next.prev = previous;
previous.next = current.next;
}
length--;
return true;
} previous = current;
current = current.next;
indexCheck++;
} return false;
}; this.remove = function(){
if(length === 0){
return false;
} var current = head,
previous,
indexCheck = 0; if(length === 1){
head = null;
tail = null;
length--;
return current.element;
} while(indexCheck++ < length){
previous = current;
current = current.next;
} previous.next = head;
tail = previous.next;
length--;
return current.element;
}; this.indexOf = function(element){
var current = head,
index = 0; while(current && index++ < length){
if(current.element === element){
return index;
}
current = current.next;
} return false;
}; this.toString = function(){
var current = head,
indexCheck = 0,
string = ''; while(current && indexCheck < length){
string += current.element;
indexCheck++;
current = current.next;
} return string;
}; this.isEmpty = function(){
return length === 0;
}; this.getHead = function(){
return head;
}; this.getTail = function(){
return tail;
}; this.size = function(){
return length;
};
}
双向链表、双向循环链表的JS实现的更多相关文章
- 数据结构8: 双向链表(双向循环链表)的建立及C语言实现
之前接触到的链表都只有一个指针,指向直接后继,整个链表只能单方向从表头访问到表尾,这种结构的链表统称为 “单向链表”或“单链表”. 如果算法中需要频繁地找某结点的前趋结点,单链表的解决方式是遍历整个链 ...
- 单链表、循环链表的JS实现
数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑...... 当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...
- JS数据结构第三篇---双向链表和循环链表之约瑟夫问题
一.双向链表 在上文<JS数据结构第二篇---链表>中描述的是单向链表.单向链表是指每个节点都存有指向下一个节点的地址,双向链表则是在单向链表的基础上,给每个节点增加一个指向上一个节点的地 ...
- 1.Go语言copy函数、sort排序、双向链表、list操作和双向循环链表
1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 package main import "fmt" func main() ...
- 1.Go-copy函数、sort排序、双向链表、list操作和双向循环链表
1.1.copy函数 通过copy函数可以把一个切片内容复制到另一个切片中 (1)把长切片拷贝到短切片中 ? 1 2 3 4 5 6 7 8 9 10 11 12 package main imp ...
- C语言通用双向循环链表操作函数集
说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低. 可基于该函数集方便地构造栈或队列集. 本函数集暂未考虑并发保护. 一 ...
- 双向循环链表的Java版本实现
1.单项循环列表 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环.和单链表相比,循环单链表的长处是从链尾到链 ...
- Linux内核中的通用双向循环链表
开发中接触Linux越来越多,休息放松之余,免不了翻看翻看神秘的Linux的内核.看到双向链表时,觉得挺有意思的,此文记下. 作为众多基础数据结构中的一员,双向循环链表在各种“教科书”中的实现是相当的 ...
- "《算法导论》之‘线性表’":双向循环链表
本文双链表介绍部分参考自博文数组.单链表和双链表介绍 以及 双向链表的C/C++/Java实现. 1 双链表介绍 双向链表(双链表)是链表的一种.和单链表一样,双链表也是由节点组成,它的每个数据结点中 ...
随机推荐
- HTML CSS 特殊字符表(转载)
转载地址:http://blog.csdn.net/bluestarf/article/details/40652011 转载原文地址:http://zhengmifan.com/news/noteb ...
- 用递归调用实现字符串反转(java版)
写一个函数,输入int型,返回整数逆序后的字符串.如:输入123,返回“321”. 要求必须用递归,不能用全局变量,输入必须是一个参数,必须返回字符串. public static String re ...
- 【splay】文艺平衡树 BZOJ 3223
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- asp:Repeater实例备忘
1.前置部分 <asp:Repeater ID="rptPlanNo" runat="server" OnItemDataBound="rptP ...
- C语言小结
1.输入输出: char s; printf("Enter a string"); scanf("%s",&s); printf(''Hello,%s& ...
- android 去掉actionbar 的虚线
if(Build.VERSION.SDK_INT>=21){ getSupportActionBar().setElevation(0); }
- MFC 创建多层目录
创建多层目录 BOOL CTestToolCtr::CreateFolder(CString strNewFolder) { /************************************ ...
- php 导出excle的.csv格式的数据时乱码问题
1.header('Content-Encoding: XXXX'); 有可能是编码问题:可以尝试UTF-8,GBK,GB2312,等编码格式 2.有可能是文件编码问题,虽然UTF-8不建议带BOM, ...
- 解决css3毛玻璃效果(blur)有白边问题
做一个登录页,全屏背景图毛玻璃效果,实现方法如下: HTML: <body> <div class="login-wrap"> <div class= ...
- Linux中的用户和用户组
在Linux中,有三种用户: Root 用户:也称为超级用户,对系统拥有完全的控制权限.超级用户可以不受限制的运行任何命令.Root 用户可以看做是系统管理员. 系统用户:系统用户是Linux运行 ...