Skip to content

Instantly share code, notes, and snippets.

@Tushkiz
Created February 27, 2014 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tushkiz/9246457 to your computer and use it in GitHub Desktop.
Save Tushkiz/9246457 to your computer and use it in GitHub Desktop.
Simple List
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