Created
April 10, 2017 07:10
-
-
Save im4aLL/a142fd2dafd3663cd937bef82579e1e6 to your computer and use it in GitHub Desktop.
LinkedList Javascript
This file contains 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
function LinkedList() { | |
this.list = null; | |
this.length = 0; | |
} | |
LinkedList.prototype.add = function(value) { | |
if( this.list === null ) { | |
this.list = this._node(value); | |
} | |
else { | |
var currentNode = this.list; | |
while(currentNode.next !== null) { | |
currentNode = currentNode.next; | |
} | |
currentNode.next = this._node(value); | |
} | |
this.length++; | |
return this; | |
} | |
LinkedList.prototype._node = function(value) { | |
return { | |
value: value, | |
next: null | |
} | |
} | |
LinkedList.prototype.toString = function(node) { | |
if(!node) { | |
node = this.list; | |
} | |
console.log(node.value); | |
if(node.next !== null) { | |
this.toString(node.next); | |
} | |
} | |
var ll = new LinkedList(); | |
ll.add(10).add(20).add(30).add('hola').toString(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment