-
-
Save manjula-dube/fe63ed275e4bda53453d0e17d4da179c to your computer and use it in GitHub Desktop.
Revisions
-
wesleyhales created this gist
Mar 12, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ //I wanted a more elegant linkedlist example than the others, so purely for academic purposes I created one. var LinkedList = function(e){ var that = {}, first, last; that.push = function(value){ var node = new Node(value); if(first == null){ first = last = node; }else{ last.next = node; last = node; } }; that.pop = function(){ var value = first; first = first.next; return value; }; that.remove = function(index) { var i = 0; var current = first, previous; if(index === 0){ //handle special case - first node first = current.next; }else{ while(i++ < index){ //set previous to first node previous = current; //set current to the next one current = current.next } //skip to the next node previous.next = current.next; } return current.value; }; var Node = function(value){ this.value = value; var next = {}; }; return that; }; var linkedList = new LinkedList(); linkedList.push(1); linkedList.push(2); linkedList.push(3); linkedList.push(4); linkedList.remove(0); console.log(linkedList.pop()); console.log(linkedList.pop()); console.log(linkedList.pop());