function Node(number) {
this.element = number;
this.prev = null;
this.next = null;
}
function DoubleLinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
DoubleLinkedList.prototype = {
constructor: DoubleLinkedList,
insert: function(pos, number) {
if (pos >= 0 && pos <= this.length) {
var node = new Node(number);
var currentNode = this.head;
var previousNode;
var index = 0;
if (pos === 0) {
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
currentNode.prev = node;
this.head = node;
}
} else if (pos === this.length) {
currentNode = this.tail;
currentNode.next = node;
node.prev = currentNode;
this.tail = node;
} else {
while (index++ < pos) {
previousNode = currentNode;
currentNode = currentNode.next;
}
node.next = currentNode;
currentNode.prev = node;
node.prev = previousNode;
previousNode.next = node;
}
this.length++;
return true;
} else {
return false;
}
}
}
console