Created
November 6, 2022 02:48
-
-
Save SparrowMike/a6289a979344902e938030346a05fa47 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.val = val; | |
this.next = null; | |
this.prev = null; | |
} | |
} | |
class DoublyLinkedList { | |
constructor() { | |
this.head = null; | |
this.tail = null; | |
this.length = 0; | |
} | |
push(val) { | |
let newNode = new Node(val); | |
if (!this.head) { | |
this.head = newNode; | |
this.tail = newNode; | |
} else { | |
this.tail.next = newNode; | |
newNode.prev = this.tail; | |
this.tail = newNode; | |
} | |
this.length++; | |
return this; | |
} | |
pop() { | |
if(!this.head) return undefined; | |
let current = this.tail; | |
if (this.length === 1) { | |
this.head = null; | |
this.tail = null; | |
} else { | |
this.tail = current.prev; | |
this.tail.next = null; | |
current.prev = null; | |
} | |
this.length--; | |
return current; | |
} | |
shift() { | |
if(!this.head) return undefined; | |
let current = this.head; | |
if (this.length === 1) { | |
this.head = null; | |
this.tail = null; | |
} else { | |
this.head = current.next; | |
this.head.prev = null; | |
current.next = null; | |
} | |
this.length--; | |
return current; | |
} | |
unshift(val) { | |
let newNode = new Node(val); | |
if (!this.head) { | |
this.head = newNode; | |
this.tail = newNode; | |
} else { | |
this.head.prev = newNode; | |
newNode.next = this.head; | |
this.head = newNode; | |
} | |
this.length++; | |
return this; | |
} | |
get(index) { | |
if(index < 0 || index >= this.length) return null; | |
let current, count; | |
if(index <= this.length / 2) { | |
current = this.head; | |
count = 0; | |
while(count !== index) { | |
current = current.next; | |
count ++; | |
} | |
} else { | |
current = this.tail; | |
count = this.length - 1; | |
while(count !== index) { | |
current = current.prev; | |
count --; | |
} | |
} | |
return current; | |
} | |
set(index, val) { | |
let node = this.get(index) | |
if (node !== null) { | |
node.val = val; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
insert(index, val) { | |
if(index < 0 || index >= this.length) return false; | |
if(index === 0) return !!this.unshift(val); | |
if(index === this.length) return !!this.push(val); | |
let newNode = new Node(val); | |
let beforeNode = this.get(index - 1); | |
let afterNode = beforeNode.next; | |
beforeNode.next = newNode, newNode.prev = beforeNode; | |
newNode.next = afterNode, afterNode.prev = newNode; | |
this.length++; | |
return true; | |
} | |
remove(index) { | |
if(index < 0 || index >= this.length) return undefined; | |
if(index === 0) return !!this.unshift(val); | |
if(index === this.length) return !!this.push(val); | |
let node = this.get(index) | |
let beforeNode = node.prev | |
let afterNode = node.next; | |
beforeNode.next = afterNode, afterNode.prev = beforeNode; | |
node.next = null, node.prev = null; | |
this.length--; | |
return node; | |
} | |
reverse() { | |
let node = this.head; | |
this.head = this.tail; | |
this.tail = node; | |
let hold; | |
for (let i = 0; i < this.length; i++) { | |
hold = node.prev | |
node.prev = node.next | |
node.next = hold | |
node = node.prev; | |
console.log(node) | |
} | |
return this | |
} | |
} | |
let list = new DoublyLinkedList(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment