Skip to content

Instantly share code, notes, and snippets.

@mayashavin
Last active January 16, 2018 21:43
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 mayashavin/51b665062bd998e1c603475d0d523398 to your computer and use it in GitHub Desktop.
Save mayashavin/51b665062bd998e1c603475d0d523398 to your computer and use it in GitHub Desktop.
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