Показать сообщение отдельно
Старый 16.10.2013, 21:34   #1
pax
Unity/C# кодер
 
Аватар для pax
 
Регистрация: 03.10.2005
Адрес: Россия, Рязань
Сообщений: 7,568
Написано 3,006 полезных сообщений
(для 5,323 пользователей)
node js связанный список

Написал связанный список для своих нужд (знаю есть готовые, но этж велосипед и опыт).

function LinkedList() {
    
this.first null;
    
this.last null;
    
this.length 0;
}

module.exports LinkedList;

LinkedList.prototype.addToStart = function (value) {
    var 
first this.first;
    var 
newItem = {
        
beforenull,
        
afterfirst,
        
valuevalue
    
};

    if (
first) {
        
first.before newItem;
    }

    if (
this.last == null) {
        
this.last newItem;
    }

    
this.first newItem;
    
this.length++;

    return 
newItem;
};
LinkedList.prototype.add =
    
LinkedList.prototype.addToEnd = function (value) {
        var 
last this.last;
        var 
newItem = {
            
beforelast,
            
afternull,
            
valuevalue
        
};

        if (
last) {
            
last.after newItem;
        }

        if (
this.first == null) {
            
this.first newItem;
        }

        
this.last newItem;
        
this.length++;

        return 
newItem;
    };


LinkedList.prototype.insertBefore = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem.before,
        
afterlistItem,
        
valuevalue
    
};

    if (
newItem.before == null) {
        
this.first newItem;
    } else {
        
newItem.before.after newItem;
    }

    
listItem.before newItem;

    
this.length++;
    return 
newItem;
};

LinkedList.prototype.insertAfter = function (valuelistItem) {
    if (!
listItem) {
        throw new 
Error('listItem == null');
    }

    var 
newItem = {
        
beforelistItem,
        
afterlistItem.after,
        
valuevalue
    
};

    if (
newItem.after == null) {
        
this.last newItem;
    } else {
        
newItem.after.before newItem;
    }

    
listItem.after newItem;

    
this.length++;

    return 
newItem;
};

LinkedList.prototype.remove = function (listItem) {

    if (
listItem.before) {
        
listItem.before.after listItem.after;
    } else {
        
this.first listItem.after;
    }

    if (
listItem.after) {
        
listItem.after.before listItem.before;
    } else {
        
this.last listItem.before;
    }
    
this.length--;
};


LinkedList.prototype.foreach = function (processItemCallbackonEndCallback) {
    var 
item this.first;

    function 
nextItemProcessor(item) {
        return function () {
            if (
item) {
                
processItemCallback(itemnextItemProcessor(item.after));
            } else {
                if (
onEndCallbackonEndCallback();
            }
        };
    }

    if (
item) {
        
processItemCallback(itemnextItemProcessor(item.after));
    } else {
        if (
onEndCallbackonEndCallback();
    }
}; 
тесты:
var util = require('util');
var 
LinkedList = require('./LinkedList');

var list = new 
LinkedList();

list.
add('one'); // добавление в конец
list.add('two'); // добавление в конец
var three = list.add("three");  // добавление в конец

var items = [];
console.log(list.length);
list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

var 
five = list.insertAfter('five'three); // вставка после

list.insertBefore('four'five); // вставка до

list.insertAfter('six'five); // вставка после

items = [];
console.log(list.length);
list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

items = [];

var 
two three.before;
list.
remove(three); // удаление
list.insertAfter(3two); // вставка после
list.remove(list.last); // удаление

console.log(list.length);

list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

list.
addToEnd(6); // добавление в конец списка
items = [];

console.log(list.length);

list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
});

list.
remove(list.first); // удаление первого элемента
list.remove(list.first); // удаление первого элемента
list.addToStart(1); // добавление в начало
list.insertAfter('два', list.first); // добавление после первого
list.insertBefore('zero', list.first); // добавление перед первым
items = [];

console.log(list.length);

list.foreach(function (
itemnext) {
    
items.push(item.value);
    
next();
}, function () {
    
console.log(util.inspect(items));
}); 
результаты тестов:
C:\nodejs\node.exe LinkedList.js
3
[ 'one', 'two', 'three' ]
6
[ 'one', 'two', 'three', 'four', 'five', 'six' ]
5
[ 'one', 'two', 3, 'four', 'five' ]
6
[ 'one', 'two', 3, 'four', 'five', 6 ]
7
[ 'zero', 1, 'два', 3, 'four', 'five', 6 ]

Process finished with exit code 0
__________________
Blitz3d to Unity Wiki

Последний раз редактировалось pax, 16.10.2013 в 23:32.
(Offline)
 
Ответить с цитированием
Эти 4 пользователя(ей) сказали Спасибо pax за это полезное сообщение:
ABTOMAT (16.10.2013), moka (16.10.2013), Phantom (17.10.2013), St_AnGer (16.10.2013)