Skip to content

Instantly share code, notes, and snippets.

@fazeelanizam13
Last active May 25, 2022 16:19
Show Gist options
  • Save fazeelanizam13/db23c980dde587b828988184406ba442 to your computer and use it in GitHub Desktop.
Save fazeelanizam13/db23c980dde587b828988184406ba442 to your computer and use it in GitHub Desktop.
Implementation of the linked list data structure in JavaScript.
class Node {
// stores given value and points to nothing
constructor (value) {
this.value = value
this.next = null
}
}
class LinkedList {
// initial size is 0 and initially just has a head node that stores nothing and points to nothing
constructor () {
this.size = 0
this.head = new Node(null)
}
// helper function to test
// prints all nodes in list in console
printNodes = () => {
let node = this.head
if (node.next) {
while (node.next !== null) {
node = node.next
console.log(node.value)
}
} else console.log('Linked list empty')
}
insertAtHead = value => {
// create new node
let node = new Node(value)
// make new node point to current first node
node.next = this.head.next
// make new node first node
this.head.next = node
this.size++
}
insertAtTail = value => {
// if no first node, means insertAtHead can do it
if (this.head.next === null) this.insertAtHead(value)
else {
// create new node
let node = new Node(value)
// initialize temp node pointing at first node
let tail = this.head.next
// traverse through the list and get to the tail
while (tail.next !== null) {
tail = tail.next
}
// we might as well skip this as new Node anyway points to null
// node.next = null
// make new node next node of current tail
tail.next = node
this.size++
}
}
insertAtIndex = (value, index) => {
// if index = 0, insertAtHead could do it
if (index === 0) this.insertAtHead(value)
// if index = size we can add node to the tail
else if (index === this.size) this.insertAtTail(value)
// if invalid index, return
else if (index > this.size) {
console.log('index greater than array size. cannot insert.')
return
}
else {
// create new node
let node = new Node(value)
// initialize temp variables
let previous = this.head
let current = previous.next
let i = 1
// traverse through list and get to node at index
while (i <= index) {
previous = previous.next
current = current.next
i++
}
// put current node after new node
node.next = current
// put new node after previous node
previous.next = node
this.size++
}
}
removeAtHead = () => {
// if no nodes, return
if (this.head.next === null) console.log("List empty.")
// initialize temp node and assign first node
let temp = this.head.next
// connect head to second node
this.head.next = temp.next
// disconnect first node from list
temp.next = null
this.size--
}
removeAtTail = value => {
// if empty list, return
if (this.head.next === null) return
else {
// initialize temp nodes pointing at head and first node
let previous = this.head
let current = previous.next
// traverse through array and get to tail node and previous node to tail node
while (current.next !== null) {
previous = previous.next
current = current.next
}
// point previous node to null
previous.next = null
this.size--
}
}
removeAtIndex = index => {
// if first node, removeAtHead
if (index === 0) this.removeAtHead()
// if tail node, removeAtTail
else if (index === this.size - 1) this.removeAtTail()
// if invalid index, return
else if (index >= this.size) {
console.log('index not found in array. cannot remove.')
return
}
else {
// initialize temp nodes and assign to head and first node
let previous = this.head
let current = previous.next
let i = 1
// traverse through list and get to node at index and previous node
while (i <= index) {
previous = previous.next
current = current.next
i++
}
// point previous node to the next node to current
previous.next = current.next
// point current node to null
current.next = null
this.size--
}
}
getAtIndex = index => {
// if first node
if (index === 0) {
// if first node exists, return it's value
if (this.head.next) return this.head.next.value
// if no first node, return null
else {
console.log('index not found in array.')
return null
}
}
// if invalid index, return null
else if (index >= this.size) {
console.log('index not found in array.')
return null
}
else {
// initialize temp node and assign to first node
let node = this.head.next
let i = 0
// traverse through list and get to node at index
while (i < index) {
node = node.next
i++
}
// return node value
return node.value
}
}
}
// tests
// let list = new LinkedList()
// list.insertAtHead(2)
// list.insertAtHead(7)
// list.insertAtTail(17)
// list.insertAtTail(8)
// list.insertAtTail(82)
// list.insertAtTail(5)
// console.log(list.getAtIndex(3))
// list.removeAtIndex(3)
// list.insertAtIndex(3, 5)
// list.removeAtIndex(3)
// list.removeAtIndex(3)
// list.removeAtTail()
// list.printNodes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment