Created
February 13, 2024 05:07
-
-
Save wmantly/fdc9d3a4794456ebf6fb72a88f2ecfa1 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(value, next){ | |
this.value = value; | |
this.next = null; | |
} | |
} | |
class LinkedList{ | |
constructor(...items){ | |
this.next = null; | |
this.push(...items); | |
} | |
* nodes(){ | |
let current = this.next | |
let last = this; | |
let count = 0; | |
while(current){ | |
yield [current, last, count++] | |
last = current; | |
current = current.next | |
} | |
} | |
* values(){ | |
for(let [node] of this.nodes()){ | |
yield node.value; | |
} | |
} | |
// Get items | |
walk(value){ | |
let count = 0; | |
let node = this; | |
let last = this; | |
for([node, last, count] of this.nodes()){ | |
if(node.value === value) break; | |
} | |
return { | |
node, | |
last, | |
length: count, | |
}; | |
} | |
search(value){ | |
let res = this.walk(value); | |
if(res.node.value === value) return node; | |
} | |
get length(){ | |
return this.walk().length+1 | |
} | |
// Add items | |
unshift(...items){ | |
let next = this.next | |
let current = this; | |
for(let item of items){ | |
current.next = new Node(item); | |
current = current.next; | |
} | |
current.next = next | |
return this; | |
} | |
push(...items){ | |
let current = this.walk().node; | |
for(let item of items){ | |
current.next = new Node(item); | |
current = current.next; | |
} | |
return this; | |
} | |
insert(value, ...items){ | |
let res = this.walk(value); | |
if(res.node.value === value){ | |
let next = res.node.next | |
let current = res.node; | |
for(let item of items){ | |
current.next = new Node(item); | |
current = current.next; | |
} | |
current.next = next; | |
} | |
return this | |
} | |
// Remove items | |
delete(value){ | |
let res = this.walk(value); | |
if(res.node.value === value){ | |
res.last.next = res.node.next; | |
return res.node.value; | |
} | |
} | |
pop(){ | |
let {last, node} = this.walk(); | |
last.next = null; | |
return node.value; | |
} | |
shift(){ | |
if(!this.next) return undefined; | |
let node = this.next; | |
this.next = this.next.next; | |
return node.value; | |
} | |
toStringReverse(current){ | |
if(current === null) return `END`; | |
if(!current){ | |
if(this.next && this.next.value){ | |
return `${this.toStringReverse(this.next)} <- HEAD ` | |
}else{ | |
return `END <- HEAD` | |
} | |
} | |
if(current){ | |
return `${this.toStringReverse(current.next)} <- | ${current.value} |` | |
} | |
} | |
toString(){ | |
let current = this.next; | |
let out = 'HEAD ->'; | |
while(current){ | |
out += ` | ${current.value} | ->`; | |
if(!current.next) break; | |
current = current.next; | |
} | |
return `${out} END`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment