Last active
January 16, 2018 21:43
-
-
Save mayashavin/51b665062bd998e1c603475d0d523398 to your computer and use it in GitHub Desktop.
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
function Node(value) { | |
this.value = value; | |
this.next = undefined; | |
this.prev = undefined; | |
} | |
function DLinkedList() { | |
var head = undefined; | |
var tail = undefined; | |
var length = 0; | |
return { | |
insert: function(item) { | |
if (!item) return; | |
var node = new Node(item); | |
if (head) { | |
node.next = head; | |
head.prev = node; | |
} | |
head = node; | |
if (!tail){ | |
tail = node; | |
} | |
length++; | |
}, | |
delete: function(value) { | |
var curr = head; //Start from head of the list | |
//Iterate through list to find the matching node | |
while (curr) { | |
if (curr.value === value){ | |
var prev = curr.prev, next = curr.next; | |
//Update the pointers | |
if (prev){ | |
prev.next = next; | |
} | |
else{ | |
head = next; //If matched node is the head | |
} | |
if (next){ | |
next.prev = prev; | |
} | |
else{ | |
tail = prev;//If matched node is the tail | |
} | |
length--; | |
break; | |
} | |
curr = curr.next; | |
} | |
}, | |
search: function(value) { | |
var curr = head; | |
var found = undefined; | |
while (curr) { | |
if (curr.value === value) { | |
found = curr; | |
break; | |
} | |
curr = curr.next; | |
} | |
return found; | |
}, | |
get size() { | |
return length; | |
}, | |
print: function() { | |
var result = []; | |
var curr = head; | |
while (curr) { | |
result.push(curr.value); | |
curr = curr.next; | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment