1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/zhoutk-jsDataStructs

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
LinkedList.js 2.5 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
zhoutk Отправлено 28.09.2015 05:44 a8293e5
/***************************************************************************
> File Name : LinkedList.js
> Author : zhoutk
> Mail : zhoutk@189.cn
> Create Time : 2015-09-18 10:23
***************************************************************************/
(function(){
"use strict";
var Node = require("./lib/SingleNode");
function LinkedList(){
this._head = new Node("This is Head Node.");
this._size = 0;
}
LinkedList.prototype.isEmpty = function(){
return this._size === 0;
};
LinkedList.prototype.size = function(){
return this._size;
};
LinkedList.prototype.getHead = function(){
return this._head;
};
LinkedList.prototype.display = function(){
var currNode = this.getHead().next;
while(currNode){
console.log(currNode.element);
currNode = currNode.next;
}
};
LinkedList.prototype.remove = function(item){
if(item) {
var preNode = this.findPre(item);
if(preNode == null)
return ;
if (preNode.next !== null) {
preNode.next = preNode.next.next;
this._size--;
}
}
};
LinkedList.prototype.add = function(item){
this.insert(item);
};
LinkedList.prototype.insert = function(newElement, item){
var newNode = new Node(newElement);
var finder = item ? this.find(item) : null;
if(!finder){
var last = this.findLast();
last.next = newNode;
}
else{
newNode.next = finder.next;
finder.next = newNode;
}
this._size++;
};
/*********************** Utility Functions ********************************/
LinkedList.prototype.findLast = function(){
var currNode = this.getHead();
while(currNode.next){
currNode = currNode.next;
}
return currNode;
};
LinkedList.prototype.findPre = function(item){
var currNode = this.getHead();
while(currNode.next !== null && currNode.next.element !== item){
currNode = currNode.next;
}
return currNode;
};
LinkedList.prototype.find = function(item){
if(item == null)
return null;
var currNode = this.getHead();
while(currNode && currNode.element !== item){
currNode = currNode.next;
}
return currNode;
};
module.exports = LinkedList;
})();

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/zhoutk-jsDataStructs.git
git@api.gitlife.ru:oschina-mirror/zhoutk-jsDataStructs.git
oschina-mirror
zhoutk-jsDataStructs
zhoutk-jsDataStructs
master