摘要:起因最近在看数据结构与算法描述,然后上去搜索,想找合适的库参考并记录下来,以备以后用时能拿来即用,最没有发现很合自己意的,于是就决定自己一一实现出来。自己的实现源代码地址
起因
最近在看《数据结构与算法--javascript描述》,然后上npmjs.org去搜索,想找合适的库参考并记录下来,以备以后用时能拿来即用,最没有发现很合自己意的,于是就决定自己一一实现出来。
npmjs相关库complex-list、smart-list
编程思路双链表多了一个指向前趋的指针,故单链表中的辅助函数findPre就不需要了;
增加了反向输出方法;
注意边界条件的处理。
DoubleNode.js
(function(){
"use strict";
function Node(element){
this.element = element;
this.next = null;
this.previous = null;
}
module.exports = Node;
})();
DoubleLinkedList.js
(function(){
"use strict";
var Node = require("./lib/DoubleNode");
function DoubleLinkedList(){
this._head = new Node("This is Head Node.");
this._size = 0;
}
DoubleLinkedList.prototype.getHead = function(){
return this._head;
};
DoubleLinkedList.prototype.isEmpty = function(){
return this._size === 0;
};
DoubleLinkedList.prototype.size = function(){
return this._size;
};
DoubleLinkedList.prototype.findLast = function(){
var currNode = this.getHead();
while(currNode.next){
currNode = currNode.next;
}
return currNode;
};
DoubleLinkedList.prototype.add = function(item){
if(item == null)
return null;
this.insert(item);
};
DoubleLinkedList.prototype.remove = function(item){
if(item) {
var node = this.find(item);
if(node == null)
return ;
if (node.next === null) {
node.previous.next = null;
node.previous = null;
} else{
node.previous.next = node.next;
node.next.previous = node.previous;
node.next = null;
node.previous = null;
}
this._size--;
}
};
DoubleLinkedList.prototype.find = function(item){
if(item == null)
return null;
var currNode = this.getHead();
while(currNode && currNode.element !== item){
currNode = currNode.next;
}
return currNode;
};
DoubleLinkedList.prototype.insert = function(newElement, item){
var newNode = new Node(newElement);
var finder = item ? this.find(item) : null;
if(!finder){
var last = this.findLast();
newNode.previous = last;
last.next = newNode;
}
else{
newNode.next = finder.next;
newNode.previous = finder;
finder.next.previous = newNode;
finder.next = newNode;
}
this._size++;
};
DoubleLinkedList.prototype.dispReverse = function(){
var currNode = this.findLast();
while(currNode != this.getHead()){
console.log(currNode.element);
currNode = currNode.previous;
}
};
DoubleLinkedList.prototype.display = function(){
var currNode = this.getHead().next;
while(currNode){
console.log(currNode.element);
currNode = currNode.next;
}
};
module.exports = DoubleLinkedList;
})();
源代码地址
https://github.com/zhoutk/js-data-struct http://git.oschina.net/zhoutk/jsDataStructs
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/92336.html
摘要:什么是双链表上一篇实战数据结构基础之单链表说到单链表由一个一个的作为节点的对象构成的,每一个节点都有指向下一个节点的指针,最后一个节点的指针域指向空。 什么是双链表? 上一篇实战PHP数据结构基础之单链表说到 单链表由一个一个的作为节点的对象构成的,每一个节点都有指向下一个节点的指针,最后一个节点的指针域指向空。每个节点可以存储任何数据类型。 而双链表每个节点有两个指针域,分别指向前驱...
摘要:一般我们都构造双向循环链表。循环链表的操作和单链表的操作基本一致,差别仅仅在于算法中的循环条件有所不同。单向循环链表双向循环链表单向循环链表是在单链表基础上,将最后一个节点的指针指向链表头。 维基百科 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构...
摘要:记得在一个公司面试上有一道题,写一个双向链表,包含链表的基本操作,插入,删除,获取长度等操作,由于时间匆忙,代码写的比较乱,连自己都没眼看了,后来细想自己从来都没有细心的写过数据结构,总觉得只要原理明白了就万事大吉了,事实证明,理论和实践还 记得在一个公司面试上有一道题,写一个双向链表,包含链表的基本操作,插入,删除,获取长度等操作,由于时间匆忙,代码写的比较乱,连自己都没眼看了,后来...
阅读 863·2021-10-09 09:41
阅读 823·2019-08-30 15:53
阅读 1277·2019-08-30 15:53
阅读 1371·2019-08-30 11:01
阅读 1815·2019-08-29 17:31
阅读 1244·2019-08-29 14:05
阅读 1936·2019-08-29 12:49
阅读 676·2019-08-28 18:17