Skip to content

Instantly share code, notes, and snippets.

@fortunee
Created March 25, 2023 00:46
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 fortunee/c6a5c0983ea309f99c0b507ade5149db to your computer and use it in GitHub Desktop.
Save fortunee/c6a5c0983ea309f99c0b507ade5149db to your computer and use it in GitHub Desktop.
Doubly Linked List
class Node {
constructor(value) {
this.previous = null;
this.value = value;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
print() {
let array = [];
let current = this.head;
for (let i = 0; i < this.length; i++) {
array.push(current.value);
current = current.next;
}
console.log(array);
}
push(value) {
const node = new Node(value);
if (!this.head) {
this.head = node;
this.tail = this.head;
} else {
this.tail.next = node;
node.previous = this.tail;
this.tail = node;
}
this.length++;
return this;
}
pop() {
if (!this.head && !this.tail) return;
const popped = this.tail;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.tail = popped.previous;
this.tail.next = null;
popped.previous = null;
}
this.length--;
return popped;
}
shift() {
if (this.length <= 0) return;
const shifted = this.head;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.head = shifted.next;
this.head.previous = null;
shifted.next = null;
}
this.length--;
return shifted;
}
unshift(value) {
const currentHead = this.head;
if (!currentHead) return this.push(value);
const node = new Node(value);
this.head = node;
this.head.next = currentHead;
currentHead.previous = this.head;
this.length++;
return this;
}
get(index) {
if (index < 0 || index >= this.length) return;
let count, current;
if (index <= this.length / 2) {
count = 0;
current = this.head;
while (count !== index) {
current = current.next;
count++;
}
} else {
count = this.length - 1;
current = this.tail;
while (count !== index) {
current = current.previous;
count--;
}
}
return current;
}
set(index, value) {
const node = this.get(index);
if (!node) return false;
node.value = value;
return true;
}
insert(index, value) {
if (index < 0 || index > this.length) return false;
if (index === 0) return !!this.unshift(value);
if (index === this.length) return !!this.push(value);
const beforeIndexNode = this.get(index - 1);
const indexNode = beforeIndexNode.next;
const newNode = new Node(value);
beforeIndexNode.next = newNode;
newNode.next = indexNode;
newNode.previous = beforeIndexNode;
indexNode.previous = newNode;
this.length++;
return true;
}
remove(index) {
if (index < 0 || index >= this.length) return;
if (index === 0) return this.shift();
if (index === this.length - 1) return this.pop();
const removedNode = this.get(index);
const currentPrevNode = removedNode.previous;
const currentNextNode = removedNode.next;
currentPrevNode.next = currentNextNode;
currentNextNode.previous = currentPrevNode;
removedNode.next = null;
removedNode.previous = null;
this.length--;
return removedNode;
}
reverse(){
if(this.length <= 0) return this;
// swap head and tail;
let current = this.head;
this.head = this.tail;
this.tail = current;
let tempNext = null;
let tempPrev = null;
for (let i = 0; i < this.length; i++) {
tempNext = current.next;
current.next = tempPrev;
current.prev = tempNext;
tempPrev = current;
current = tempNext;
}
return this;
}
}
const dll = new DoublyLinkedList();
dll.push(1);
dll.push(2);
dll.push(3);
// dll.print();
// dll.pop();
// dll.print();
// dll.shift();
// dll.print();
// dll.unshift(4);
// dll.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment