Last active
August 29, 2015 14:04
-
-
Save girish3/0ce8ed27515721f7bc42 to your computer and use it in GitHub Desktop.
Doubly Linked List
This file contains 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 ( 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