Skip to content

Instantly share code, notes, and snippets.

@toluolatubosun
Created May 27, 2022 17:36
Show Gist options
  • Save toluolatubosun/41dea4735cfe34ba454dbad779fdd2e4 to your computer and use it in GitHub Desktop.
Save toluolatubosun/41dea4735cfe34ba454dbad779fdd2e4 to your computer and use it in GitHub Desktop.
Linked List In JavaScript. This is a full implementation of linked lists in JavaScript; Insert to the left or right or at an index, get data at an index, remove by index, reverse list, clear list, print list
// Node Class
class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}
// Linked List Class
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
// Add a node to the left
/*
Make the new node the head (the first node) and the next node the current head
*/
insertLeft(data) {
this.head = new Node(data, this.head);
this.size++;
}
// Add a node to the right
/*
- if there is no element in the node make it the head
- else loop through the linked list (while there is another node after); then attach the node as the next node, to the last node
*/
insertRight(data) {
var node = new Node(data);
if (!this.head) {
this.head = node;
} else {
var item = this.head;
while (item.next) {
item = item.next;
}
item.next = node;
}
this.size++;
}
// Add a node at an index
/*
- if the index is out of range, insert it to the right
- if the index is 0 or less, insert it to the left
- Loop to the index and keep track of the current node and previous node in the loop
- then put the node in between the previous and current node
*/
insertAtIndex(index, data) {
if (index > this.size - 1) {
this.insertRight(data);
return;
}
if (index <= 0) {
this.insertLeft(data);
return;
}
// loop index variable
let count = 0;
let currentNode = this.head;
let previousNode;
while (count < index) {
previousNode = currentNode;
currentNode = currentNode.next;
count++;
}
let node = new Node(data, currentNode);
previousNode.next = node;
this.size++;
}
// Get At index
/*
- if index is out of range show warning message and retrun
- loop unitl index is gotten the log the data, update the node to the next node in the loop
*/
getAtIndex(index) {
if (index < 0 || index > this.size - 1) {
console.warn(`Out Of Range. List size: ${this.size}`);
return;
}
let count = 0;
let node = this.head;
while (true) {
if (count == index) {
console.log("Found:", node.data);
break;
}
node = node.next;
count++;
}
}
// Remove at index
/*
- if index is out of range show warning message and retrun
- if index is 0 update the head, with the next node
- else run a loop until the index is gotten to; then update the next previous node to point to, the node after the current node
*/
removeAtIndex(index) {
if (index < 0 || index > this.size - 1) {
console.warn(`Out Of Range. List size: ${this.size}`);
return;
}
let count = 0;
let currentNode = this.head;
let previusNode = null;
if (index == 0) {
this.head = currentNode.next;
} else {
while (true) {
if (count == index) {
previusNode.next = currentNode.next;
break;
}
previusNode = currentNode;
currentNode = currentNode.next;
count++;
}
}
this.size--;
}
// Reverse Linked List
/*
- loop from head
- While current node is not null, the current node should point to the previous node
also shift the previous and current node forward for the next iteration
- Then previous node is now the head, because the previous node is now at the last element in the list
(which points to the current node 'null') and the connections have been reversed
*/
reverseList() {
var currentNode = this.head;
var previousNode = null;
while (currentNode) {
let next = currentNode.next;
currentNode.next = previousNode;
// Perpare for next iteration
previousNode = currentNode;
currentNode = next;
}
this.head = previousNode;
}
// Print Linked List
/*
Loop through the linked list and print the data
*/
printList() {
var node = this.head;
while (node) {
console.log(node.data);
node = node.next;
}
}
// Clear list
/*
Set the head to null and size to zero
*/
clearList() {
this.head = null;
this.size = 0;
}
}
const list = new LinkedList();
list.insertLeft("A");
list.insertRight("B");
list.insertRight("D");
list.insertRight("E");
list.insertAtIndex(2, "C");
list.getAtIndex(2);
list.removeAtIndex(4);
list.reverseList();
list.printList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment