18 Star 19 Fork 2

zhoutk/jsDataStructs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DoubleLinkedList.js 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
zhoutk 提交于 2015-09-28 10:44 . add push target.
/***************************************************************************
> File Name : DoubleLinkedList.js
> Author : zhoutk
> Create Time : 2015-09-25 12:05
***************************************************************************/
(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;
})();
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/zhoutk/jsDataStructs.git
[email protected]:zhoutk/jsDataStructs.git
zhoutk
jsDataStructs
jsDataStructs
master

搜索帮助