Last active
November 2, 2023 15:31
-
-
Save tulusibrahim/7e0253d2a2c40d91708c0d8a66a3de23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(val) { | |
this.prev = null; | |
this.val = val; | |
this.next = null; | |
} | |
} | |
class DoublyLL { | |
#size = 0; | |
#head = 0; | |
#tail = 0; | |
constructor() { | |
this.#tail = null; | |
this.#size = 0; | |
this.#head = null; | |
} | |
get(index) { | |
if (index >= this.#size) throw new Error('Index is out of bound'); | |
let curr = this.#head | |
let count = 0 | |
while(count < index) { | |
curr = curr.next | |
count++ | |
} | |
return curr.val | |
} | |
append(val) { | |
this.#size += 1; | |
const node = new Node(val); | |
if (this.#tail == null) { | |
this.#head = node; | |
this.#tail = node; | |
return; | |
} | |
node.prev = this.#tail; | |
this.#tail.next = node; | |
this.#tail = node; | |
} | |
prepend(val) { | |
this.#size += 1; | |
const node = new Node(val); | |
if (this.#head == null) { | |
this.#tail = node; | |
this.#head = node; | |
return; | |
} | |
node.next = this.#head; | |
this.#head.prev = node; | |
this.#head = node; | |
} | |
insertAt(val, idx) { | |
if (idx > this.#size) throw new Error('Index is out of bound'); | |
let curr = this.#head; | |
const node = new Node(val); | |
let counter = 1; | |
if (idx == 0) { | |
this.prepend(val); | |
return | |
} | |
this.#size += 1 | |
while (curr.next) { | |
if (counter == idx) break; | |
counter += 1; | |
curr = curr.next; | |
} | |
node.next = curr.next; | |
curr.next.prev = node | |
curr.next = node; | |
node.prev = curr; | |
} | |
removeHead() { | |
this.#head = this.#head.next | |
this.#size -= 1 | |
} | |
removeTail() { | |
this.#tail = this.#tail.prev | |
this.#tail.next = null | |
this.#size -= 1 | |
} | |
removeAt(index) { | |
let count = 1; | |
let curr = this.#head; | |
if(index >= this.#size) throw new Error('Index is out of bound') | |
if(index == 0) { | |
this.removeHead() | |
} | |
while (curr.next && count < index) { | |
curr = curr.next; | |
count++ | |
} | |
curr.next = curr.next.next; | |
this.#size-=1 | |
} | |
entries() { | |
let res = []; | |
let curr = this.#head; | |
while (curr.next) { | |
res.push(curr.val); | |
curr = curr.next; | |
} | |
res.push(curr.val); | |
return res; | |
} | |
length() { | |
return this.#size | |
} | |
} | |
const dll = new DoublyLL(); | |
dll.append(1); | |
dll.append(2); | |
dll.append(3); | |
dll.append(4); | |
dll.prepend(5); | |
dll.insertAt(10, 2); | |
console.log('after adding: ',dll.entries()); | |
dll.removeHead() | |
dll.removeTail() | |
console.log('after remove head, tail: ', dll.entries()); | |
dll.removeAt(2) | |
console.log('after remove at index 2: ',dll.entries()); | |
console.log('length: ', dll.length()); | |
console.log('val at id 1: ',dll.get(1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment