Linked List
The Cabin at Saint-Adresse, 1876, Claude Monet
1. What is Linked List?
Linked List insert operation
Linked List append operation
2. Linked List Implementation
// title: 'LinkedList.js'
class Node {
constructor(element) {
this.element = element;
this.next = null;
};
}
class LinkedList {
constructor() {
this.length = 0;
this.head = null;
}
size(){
return this.length;
};
head(){
return this.head;
};
add(element){
let node = new Node(element);
if(this.head === null){
this.head = node;
} else {
let currentNode = this.head;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = node;
}
this.length++;
};
remove(element){
let currentNode = this.head;
let previousNode;
if(currentNode.element === element){
this.head = currentNode.next;
} else {
while(currentNode.element !== element) {
previousNode = currentNode;
currentNode = currentNode.next;
}
previousNode.next = currentNode.next;
}
this.length --;
};
isEmpty() {
return this.length === 0;
};
indexOf(element) {
let currentNode = this.head;
let index = -1;
while(currentNode){
index++;
if(currentNode.element === element){
return index;
}
currentNode = currentNode.next;
}
return -1;
};
elementAt(index) {
let currentNode = this.head;
let count = 0;
while (count < index){
count ++;
currentNode = currentNode.next
}
return currentNode.element;
};
addAt(index, element){
let node = new Node(element);
let currentNode = this.head;
let previousNode;
let currentIndex = 0;
if(index > this.length){
return false;
}
if(index === 0){
node.next = currentNode;
this.head = node;
} else {
while(currentIndex < index){
currentIndex++;
previousNode = currentNode;
currentNode = currentNode.next;
}
node.next = currentNode;
previousNode.next = node;
}
this.length++;
}
removeAt(index) {
let currentNode = this.head;
let previousNode;
let currentIndex = 0;
if (index < 0 || index >= this.length){
return null
}
if(index === 0){
this.head = currentNode.next;
} else {
while(currentIndex < index) {
currentIndex ++;
previousNode = currentNode;
currentNode = currentNode.next;
}
previousNode.next = currentNode.next
}
this.length--;
return currentNode.element;
}
}
let conga = new LinkedList();
conga.add('Kitten');
conga.add('Puppy');
conga.add('Dog');
conga.add('Cat');
conga.add('Fish');
console.log(conga.size()); // 5
console.log(conga.removeAt(3)); // Cat
console.log(conga.elementAt(3)); // Fish
console.log(conga.indexOf('Puppy')); // 1
console.log(conga.size()); // 4
https://www.freecodecamp.org/
https://www.codesdope.com/
https://algorithmtutor.com/
https://blog.penjee.com/learnprogramming/programming-gifs/
https://dev.to/abdisalan_js/4-ways-to-traverse-binary-trees-with-animations-5bi5
https://www.programiz.com/