class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor(value) {
this.head = {
value,
next: null,
prev: null,
};
this.tail = this.head;
this.length = 1;
}
append(value) {
// O(1)
const newNode = new Node(value);
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
this.length++;
}
prepend(value) {
// O(1)
const newNode = new Node(value);
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
this.length++;
}
printList() {
const array = [];
let currentNode = this.head;
while (currentNode !== null) {
array.push(currentNode.value);
currentNode = currentNode.next;
}
return array;
}
insert(index, value) {
if (index === 0) {
return this.prepend(value);
}
if (index >= this.length) {
return this.append(value);
}
let count = 0;
let prev = this.head;
const newNode = new Node(value);
while (prev.next !== null) {
if (count === index - 1) {
newNode.next = prev.next;
prev.next.prev = newNode;
newNode.prev = prev;
prev.next = newNode;
this.length++;
break;
}
prev = prev.next;
count++;
}
}
remove(index) {
if (index === 0) {
const newHead = this.head.next;
delete this.head;
this.head = newHead;
return;
}
let count = 0;
let prev = this.head;
while (prev.next !== null) {
if (count === index - 1) {
const deleteThis = prev.next;
delete prev.next;
deleteThis.next.prev = prev;
prev.next = deleteThis.next;
this.length--;
break;
}
prev = prev.next;
count++;
}
}
}
const myDoublyLinkedList = new DoublyLinkedList(10);
console.log(myDoublyLinkedList.printList());
myDoublyLinkedList.append(50);
console.log(myDoublyLinkedList.printList());
myDoublyLinkedList.append(60);
console.log(myDoublyLinkedList.printList());
myDoublyLinkedList.append(70);
console.log(myDoublyLinkedList.printList());
myDoublyLinkedList.append(80);
console.log(myDoublyLinkedList.printList());
myDoublyLinkedList.prepend(1);
console.log(myDoublyLinkedList.printList());
myDoublyLinkedList.insert(1, 99);
console.log(myDoublyLinkedList.printList());
myDoublyLinkedList.remove(0);
console.log(myDoublyLinkedList.printList());
console.log(myDoublyLinkedList);
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor(value) {
this.head = {
value,
next: null,
prev: null,
};
this.tail = this.head;
this.length = 1;
}
append(value) {
// O(1)
const newNode = new Node(value);
newNode.prev = this.tail;
this.tail.next = newNode;
this.tail = newNode;
this.length++;
}
prepend(value) {
// O(1)
const newNode = new Node(value);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
this.length++;
}
printList() {
const array = [];
let currentNode = this.head;
while (currentNode !== null) {
array.push(currentNode.value);
currentNode = currentNode.next;
}
return array;
}
insert(index, value) {
// check parmas
if (index === 0) {
return this.prepend(value);
}
if (index >= this.length) {
return this.append(value);
}
const newNode = {
value,
next: null,
};
const leader = this.traverseToIndex(index - 1);
const follower = leader.next;
leader.next = newNode;
newNode.next = follower;
newNode.prev = leader;
follower.prev = newNode;
this.length++;
}
traverseToIndex(index) {
// O(n)
// check params
let counter = 0;
let currentNode = this.head;
while (counter !== index) {
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
remove(index) {
// check params
if (index === 0) {
const unwantedNode = this.head;
unwantedNode.next.prev = null;
this.head = unwantedNode.next;
this.length--;
return this.printList();
}
if (index >= this.length) {
return this.printList();
}
const leader = this.traverseToIndex(index - 1);
const unwantedNode = leader.next;
if (unwantedNode.next) {
unwantedNode.next.prev = leader;
}else {
this.tail = leader;
}
leader.next = unwantedNode.next;
this.length--;
return this.printList();
}
}
const myLinkedList = new DoublyLinkedList(10);
myLinkedList.append(50);
// myLinkedList.append(60);
// myLinkedList.append(70);
// myLinkedList.append(80);
// myLinkedList.prepend(1);
// myLinkedList.insert(3, 99);
console.log(myLinkedList.printList());
console.log(myLinkedList.remove(1));
console.log(myLinkedList);