Created
March 25, 2023 00:47
-
-
Save fortunee/f500d0ee7ec914648b2bb52e192b88e4 to your computer and use it in GitHub Desktop.
Singly Linked List
This file contains hidden or 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) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
class singlyLinkedList { | |
constructor() { | |
this.length = 0; | |
this.head = null; | |
this.tail = null; | |
} | |
push(value) { | |
const newNode = new Node(value); | |
if (!this.head) { | |
this.head = newNode; | |
this.tail = this.head; | |
} else { | |
this.tail.next = newNode; | |
this.tail = newNode; | |
} | |
this.length++; | |
return this; | |
} | |
pop() { | |
if (!this.head) return; | |
let current = this.head; | |
if (this.length === 1) { | |
this.head = null; | |
this.tail = null; | |
this.length--; | |
return current; | |
} | |
let temp = current; | |
while (current && current.next) { | |
temp = current; | |
current = current.next; | |
} | |
this.tail = temp; | |
this.tail.next = null; | |
this.length--; | |
return temp; | |
} | |
shift() { | |
if (!this.head) return; | |
let currentHead = this.head; | |
this.head = currentHead.next; | |
this.length--; | |
if (this.length <= 0) this.tail = null; | |
return currentHead; | |
} | |
unshift(value) { | |
const node = new Node(value); | |
const currentHead = this.head; | |
if (!currentHead) { | |
this.head = node; | |
this.tail = this.head; | |
} else { | |
node.next = currentHead; | |
this.head = node; | |
} | |
this.length++; | |
return this; | |
} | |
get(index) { | |
if (index < 0 || index >= this.length) return; | |
let currentNodeIndex = 0; | |
let currentNode = this.head; | |
while (currentNodeIndex < index) { | |
currentNode = currentNode.next; | |
currentNodeIndex++ | |
} | |
return currentNode; | |
} | |
set(value, index) { | |
const node = this.get(index); | |
if (!node) return false; // throw new Error('Invalid index'); | |
node.value = value; | |
return true; | |
} | |
insert(value, index) { | |
if (index < 0 || index > this.length) return false; | |
if (index === 0) { | |
return !!this.unshift(value); | |
} | |
if (index === this.length) { | |
return !!this.push(value); | |
} | |
const previousNode = this.get(index - 1); | |
const newNode = new Node(value); | |
newNode.next = previousNode.next; | |
previousNode.next = 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 previousNode = this.get(index - 1); | |
const removed = previousNode.next; | |
previousNode.next = removed.next; | |
this.length--; | |
return removed; | |
} | |
print() { | |
const array = []; | |
let current = this.head; | |
while(current) { | |
array.push(current.value); | |
current = current.next; | |
} | |
// const array = Array.from({ length: this.length }).map(() => { | |
// const val = current.value; | |
// current = current.next; | |
// return val; | |
// }); | |
console.log(array); | |
} | |
reverse() { | |
let currentNode = this.head; | |
this.head = this.tail; | |
this.tail = currentNode; | |
let next, prev = null; | |
// for (let i = 0; i < this.length; i++) { | |
while (currentNode) { | |
next = currentNode.next; | |
currentNode.next = prev; | |
prev = currentNode; | |
currentNode = next | |
} | |
return this; | |
} | |
} | |
const list = new singlyLinkedList(); | |
list.push('Hey there!') | |
list.push('Sup') | |
list.push('Im alright') | |
list.push('Whatever!') | |
// list.pop(); | |
// list.shift(); | |
// list.unshift('Chairman') | |
// console.log(list); | |
// console.log(list.get(4)) | |
// list.set('Chairwoman', 1); | |
// console.log(list) | |
// list.insert('New item', 12); | |
// console.log(list) | |
// list.remove(2); | |
// console.log(list) | |
list.print(); | |
list.reverse(); | |
list.print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment