Created
April 27, 2012 09:08
-
-
Save deholic/2507670 to your computer and use it in GitHub Desktop.
Javascript LinkedList (not tested!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var node = function(text) { | |
var pointers = { _next: null }; | |
var data = { text: null }; | |
var _const = function() { | |
this.data.text = text; | |
}; | |
if(!!text) _const(); | |
return { | |
getText: function(text) { | |
return !!data.text ? data.text : null; | |
}, | |
setText: function(text) { | |
if(!!text) data.text = text; | |
}, | |
getNext: function() { | |
return !!pointers._next ? pointers._next : null; | |
}, | |
setNextNode: function(node) { | |
if(!!pointers._next) pointers._next = node; | |
} | |
}; | |
}; | |
var linkedList = function(node) { | |
var list = { | |
_start:null, | |
_prev:null, | |
_current:null, | |
_getNextNode:function () { | |
if (list._start === null) return null; | |
list._prev = list._current; | |
list._current = list._current.getNext(); | |
return list._current; | |
}, | |
_resetNodes:function () { | |
list._current = list._start; | |
list._prev = null; | |
} | |
}; | |
list._start = !!node ? node : new node(""); | |
list._current = list._start; | |
return { | |
getCurrentNode: function() { | |
return list._current; | |
}, | |
deleteNode:function (text) { | |
if (list._start === null || text === null) return; | |
list._current = list._start; | |
do { | |
if (list._current.getText() === text) | |
list._prev.setNextNode(list._getNextNode()); | |
} while (list._getNextNode() !== null); | |
}, | |
addNode:function (text) { | |
var text = text || ''; | |
while (list._getNextNode() !== null) { | |
} | |
list._current.setNextNode(new Node(text)); | |
}, | |
findNode:function (text) { | |
if (list._start === null) return null; | |
var targetNode = null; | |
do { | |
if (list._current.getText() === text) targetNode = list._current; | |
} while (list._getNextNode() !== null); | |
return targetNode; | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment