Last active
April 5, 2017 20:30
-
-
Save sushiljainam/f77d5314a8a4878192125432d49af6e1 to your computer and use it in GitHub Desktop.
linked list using JS, simple
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 (value){ | |
this.value = value; | |
this.next = null; | |
return this; | |
} | |
var list = function (){ | |
this.head = null; | |
return this; | |
} | |
list.prototype.last = function (){ | |
var c = this.head; | |
while(c&&c.next){ | |
c = c.next; | |
} | |
return c; | |
}; | |
list.prototype.push = function(value){ | |
var n = new node(value); | |
if(!!this.last()) { | |
this.last().next = n; | |
} else { | |
this.head = n; | |
} | |
return this; | |
}; | |
list.prototype.print = function (){ | |
var c = this.head; | |
while(c){ | |
console.log(c.value); | |
c = c.next; | |
} | |
return true; | |
}; | |
list.prototype.length = function (){ | |
var l = 0,c = this.head; | |
while(c){ | |
c = c.next;l++; | |
} | |
return l; | |
}; | |
//---- TEST ---- | |
ll.push(5).push(7); | |
ll.push(4); | |
ll.last(); | |
ll.print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment