Last active
April 27, 2021 17:37
-
-
Save passandscore/b8f76ea1652d24078421bee3fb6a8771 to your computer and use it in GitHub Desktop.
DataStructure - Linked List Principles
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
//Algorithms - Linked List Principles | |
//Language: JavaScript | |
class Node { | |
constructor(data, next = null) { | |
this.data = data; | |
this.next = next; | |
} | |
} | |
// const n1 = new Node(100) | |
class LinkedList { | |
constructor() { | |
this.head = null; | |
this.size = 0; | |
} | |
//insert first node | |
insertFirst(data) { | |
this.head = new Node(data, this.head); | |
this.size++; | |
} | |
//insert last node | |
insertLast(data) { | |
let node = new Node(data); | |
let current; | |
//if empty, make head | |
if (!this.head) { | |
this.head = node; | |
} else { | |
current = this.head; | |
while (current.next) { | |
current = current.next; | |
} | |
current.next = node; | |
} | |
this.size++; | |
} | |
insertAt(data, index) { | |
//If index is out of range | |
if (index > 0 && index > this.size) { | |
return | |
} | |
//if first index | |
if (index === 0) { | |
this.head = new Node(data, this.head); | |
return | |
} | |
const node = new Node(data); | |
let current, previous; | |
//set current to first | |
current = this.head; | |
let count = 0; | |
while (count < index) { | |
previous = current; //node before index | |
count++; | |
current = current.next; // node after index | |
} | |
node.next = current; | |
previous.next = node; | |
this.size++; | |
} | |
//get at index | |
getAt(index) { | |
let current = this.head; | |
let count = 0; | |
while (current) { | |
if (count == index) { | |
console.log(current.data); | |
} | |
count++; | |
current = current.next; | |
} | |
return null; | |
} | |
getMiddleElement() { | |
let slow = this.head; | |
let fast = this.head; | |
while (fast.next && fast.next.next) { | |
slow = slow.next; | |
fast = fast.next.next; | |
} | |
console.log(slow.data); | |
} | |
getLastElement() { | |
if (!this.head) { | |
return null; | |
} | |
let node = this.head; | |
while (node) { | |
if (!node.next) { | |
return node; | |
} | |
node = node.next; | |
} | |
} | |
//remove at index | |
removeAt(index) { | |
if (index > 0 && index > this.size) { | |
return; | |
} | |
let current = this.head; | |
let previous; | |
let count = 0; | |
//remove first | |
if (index === 0) { | |
this.head = current.next; | |
} else { | |
while (count < index) { | |
count++; | |
previous = current; | |
current = current.next; | |
} | |
previous.next = current.next; | |
} | |
this.size--; | |
} | |
//clear the list | |
clearList() { | |
this.head = null; | |
this.size = 0; | |
} | |
//print the list | |
printListData() { | |
let current = this.head; | |
while (current) { | |
console.log(current.data); | |
current = current.next; | |
} | |
} | |
} | |
const ll = new LinkedList(); | |
ll.insertFirst(100); | |
ll.insertFirst(200); | |
ll.insertFirst(300); | |
ll.insertLast(400); | |
ll.insertAt(500, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment