Last active
April 10, 2021 08:05
-
-
Save ComradeCat24/61c758e50e65d49f21921e533d4eaf7b 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 SinglyLinkedListNode { | |
constructor(data, next = null) { | |
this.data = data; // piece of data | |
this.next = next; //reference to next node | |
} | |
} | |
class SinglyLinkedList { | |
constructor() { | |
this.head = null; | |
this.tail = null; | |
this.length = 0; | |
} | |
/* | |
⁑ Pushing ⁑ | |
» Adding a new node to the end of the Linked List! | |
♯ Pseudocode | |
This function should accept a value | |
Create a new node using the value passed to the function | |
If there is no head property on the list, set the head and tail to be the newly created node | |
Otherwise set the next property on the tail to be the new node and set the tail property on the list to be the newly created node | |
Increment the length by one | |
Return the linked list | |
*/ | |
push(data) { | |
const node = new SinglyLinkedListNode(data); | |
if (!this.head) this.head = node; | |
else this.tail.next = node; | |
this.tail = node; | |
this.length++; | |
return this; | |
} | |
/* | |
⁑ Popping ⁑ | |
» Removing a node from the end of the Linked List! | |
♯ Pseudocode | |
If there are no nodes in the list, return undefined | |
Loop through the list until you reach the tail | |
Set the next property of the 2nd to last node to be null | |
Set the tail to be the 2nd to last node | |
Decrement the length of the list by 1 | |
Return the value of the node removed | |
*/ | |
pop() { | |
if (!this.head) return undefined; | |
let removedNode; | |
if (this.head === this.tail) { | |
removedNode = this.head; | |
this.head = null; | |
this.tail = null; | |
} else { | |
let currentNode = this.head; | |
while (currentNode.next !== this.tail) { | |
currentNode = currentNode.next; | |
} | |
removedNode = currentNode.next; | |
currentNode.next = null; | |
this.tail = currentNode; | |
} | |
this.length--; | |
return removedNode; | |
} | |
/* | |
⁑ Shifting ⁑ | |
» Removing a new node from the beginning of the Linked List! | |
♯ Pseudocode | |
If there are no nodes, return undefined | |
Store the current head property in a variable | |
Set the head property to be the current head's next property | |
Decrement the length by 1 | |
Return the value of the node removed | |
*/ | |
shift() { | |
if (!this.head) return undefined; | |
const removedNode = this.head; | |
if (this.head === this.tail) this.tail = null; | |
this.head = removedNode.next; | |
this.length--; | |
return removedNode; | |
} | |
/* | |
⁑ Unshifting ⁑ | |
» Adding a new node to the beginning of the Linked List! | |
♯ Pseudocode | |
This function should accept a value | |
Create a new node using the value passed to the function | |
If there is no head property on the list, set the head and tail to be the newly created node | |
Otherwise set the newly created node's next property to be the current head property on the list | |
Set the head property on the list to be that newly created node | |
Increment the length of the list by 1 | |
Return the linked list | |
*/ | |
unshift(data) { | |
this.head = new SinglyLinkedListNode(data, this.head); | |
if (!this.length) this.tail = this.head; | |
this.length++; | |
return this; | |
} | |
/* | |
⁑ Get ⁑ | |
» Retrieving a node by it's position in the Linked List! | |
♯ Pseudocode | |
This function should accept an index | |
If the index is less than zero or greater than or equal to the length of the list, return null | |
Loop through the list until you reach the index and return the node at that specific index | |
*/ | |
get(index) { | |
if (index < 0 || index >= this.length) return null; | |
let currentNode = this.head; | |
for (let i = 1; i <= index; i++) { | |
currentNode = currentNode.next; | |
} | |
return currentNode; | |
} | |
/* | |
⁑ Set ⁑ | |
» Changing the value of a node based on it's position in the Linked List | |
♯ Pseudocode | |
This function should accept a value and an index | |
Use your get function to find the specific node. | |
If the node is not found, return false | |
If the node is found, set the value of that node to be the value passed to the function and return true | |
*/ | |
set(index, data) { | |
const node = this.get(index); | |
if (!node) return false; | |
node.data = data; | |
return true; | |
} | |
/* | |
⁑ Insert ⁑ | |
» Adding a node to the Linked List at a specific position | |
♯ Pseudocode | |
If the index is less than zero or greater than the length, return false | |
If the index is the same as the length, push a new node to the end of the list | |
If the index is 0, unshift a new node to the start of the list | |
Otherwise, using the get method, access the node at the index - 1 | |
Set the next property on that node to be the new node | |
Set the next property on the new node to be the previous next | |
Increment the length | |
Return true | |
*/ | |
insert(index, data) { | |
if (index < 0 || index > this.length) return false; | |
if (index === 0) return !!this.unshift(data); | |
if (index === this.length) return !!this.push(data); | |
const prevNode = this.get(index - 1); | |
if (!prevNode) return false; | |
prevNode.next = new SinglyLinkedListNode(data, prevNode.next); | |
this.length++; | |
return true; | |
} | |
/* | |
⁑ Remove ⁑ | |
» Removing a node from the Linked List at a specific position | |
♯ Pseudocode | |
If the index is less than zero or greater than the length, return undefined | |
If the index is the same as the length-1, pop | |
If the index is 0, shift | |
Otherwise, using the get method, access the node at the index - 1 | |
Set the next property on that node to be the next of the next node | |
Decrement the length | |
Return the value of the node removed | |
*/ | |
remove(index) { | |
if (index < 0 || index >= this.length) return undefined; | |
if (index === 0) return this.shift(); | |
if (index === this.length - 1) return this.pop(); | |
const prevNode = this.get(index - 1); | |
if (!prevNode) return undefined; | |
const removedNode = prevNode.next; | |
prevNode.next = removedNode.next; | |
this.length--; | |
return removedNode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment