# The Doubly Linked List data structure

## #data-structures series

Posted on January 13, 2016

The #data-structures series is a collection of posts about reimplemented data structures in JavaScript.

If you are not familiar with data structures, a quick introduction and the full list of reimplemented data structures can be found in the introduction post of the series on data structures in JavaScript.

If you feel comfortable with the concept of each data structure and only want to see the code, have a look at the summary post of the series. It removes all explanations and contains only the JavaScript code for all data structures discussed in the series.

## Get the code on Github

Of course, all the code can also be found on Github in the repository data-structures-in-javascript.

# The Doubly Linked List data structure

## Definition

A Doubly Linked List is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. From Wikipedia

Having two node links allow traversal in either direction but adding or removing a node in a doubly linked list requires changing more links than the same operations on a Singly Linked List.

## Complexity

Average
Access Search Insertion Deletion
O(n) O(n) O(1) O(1)

To get a full overview of the time and space complexity of the Doubly Linked List data structure, have a look to this excellent Big O cheat sheet.

## The code

``````function Node(data) {
this.data = data;
this.previous = null;
this.next = null;
}

this.tail = null;
this.numberOfValues = 0;
}

var node = new Node(data);
this.tail = node;
} else {
node.previous = this.tail;
this.tail.next = node;
this.tail = node;
}
this.numberOfValues++;
};
while(current) {
if(current.data === data) {
if(current === this.head && current === this.tail) {
this.tail = null;
} else if(current === this.head) {
} else if(current === this.tail) {
this.tail = this.tail.previous;
this.tail.next = null;
} else {
current.previous.next = current.next;
current.next.previous = current.previous;
}
this.numberOfValues--;
}
current = current.next;
}
};
while(current) {
if(current.data === toNodeData) {
var node = new Node(data);
if(current === this.tail) {
} else {
current.next.previous = node;
node.previous = current;
node.next = current.next;
current.next = node;
this.numberOfValues++;
}
}
current = current.next;
}
};
while(current) {
if(fn) {
fn(current);
}
current = current.next;
}
};
var current = this.tail;
while(current) {
if(fn) {
fn(current);
}
current = current.previous;
}
};
return this.numberOfValues;
};
var string = '';
while(current) {
string += current.data + ' ';
current = current.next;
}
console.log(string.trim());
};

doublyLinkedList.print(); // => 1 2 3 4
console.log('length is 4:', doublyLinkedList.length()); // => 4
doublyLinkedList.print(); // => 1 2 4
doublyLinkedList.remove(9); // remove non existing value
doublyLinkedList.print(); // => 1 2 4
console.log('length is 1:', doublyLinkedList.length()); // => 1
doublyLinkedList.remove(2); // remove tail, the list should be empty
console.log('length is 0:', doublyLinkedList.length()); // => 0
doublyLinkedList.print(); // => 2 3 6
doublyLinkedList.print(); // => 2 3 4 6
doublyLinkedList.insertAfter(5, 9); // insertAfter a non existing node
doublyLinkedList.print(); // => 2 3 4 6