Created
February 27, 2014 08:35
-
-
Save Tushkiz/9246457 to your computer and use it in GitHub Desktop.
Simple List
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
function List() { | |
this.head = this.tail = null | |
} | |
List.prototype.add = function add(node) { | |
if (this.head) { | |
node.next = this.head; | |
this.head.prev = node; | |
} | |
this.head = node; | |
this.tail = this.tail || node; | |
}; | |
List.prototype.remove = function remove(node) { | |
node.prev ? node.prev.next = node.next : this.head = node.next; | |
node.next ? node.next.prev = node.prev : this.tail = node.prev; | |
}; | |
List.prototype.moveToFront = function(node) { | |
this.remove(node); | |
this.add(node); | |
}; | |
List.prototype.print = function() { | |
var temp = this.head; | |
while(temp) { | |
console.log(temp.val) | |
temp = temp.next; | |
}; | |
}; | |
function Node(key, val) { | |
this.key = key; | |
this.val = val; | |
this.prev = this.next = null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment