Skip to content

Instantly share code, notes, and snippets.

@girish3
Last active August 29, 2015 14:04
Show Gist options
  • Save girish3/0ce8ed27515721f7bc42 to your computer and use it in GitHub Desktop.
Save girish3/0ce8ed27515721f7bc42 to your computer and use it in GitHub Desktop.
Doubly Linked List
var Node = function ( data ) {
this.data = data;
this.next = null;
this.previous = null;
}
var LinkedList = function () {
this.first = null;
this.last = null;
this.length = 0;
}
LinkedList.prototype = {
append: function ( data ) {
var node = new Node( data );
if ( this.first != null ) {
this.last.next = node
node.previous = this.last
this.last = node;
}
else {
this.first = node;
this.last = node;
}
this.length++ ;
},
remove: function ( node ) {
var previous = node.previous,
next = node.next;
if ( previous !== null && next !== null ) {
previous.next = next;
next.previous = previous;
}
else if ( previous === null && next !== null ) {
this.first = next;
next.previous = null;
}
else if ( previous !== null && next === null ) {
this.last = previous;
previous.next = null;
}
else {
this.first = null;
this.last = null;
}
this.length-- ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment