Skip to content

Instantly share code, notes, and snippets.

@khaled0fares
Last active September 23, 2016 14:36
Show Gist options
  • Save khaled0fares/5314dbcba935e3ed29f276a251e6f197 to your computer and use it in GitHub Desktop.
Save khaled0fares/5314dbcba935e3ed29f276a251e6f197 to your computer and use it in GitHub Desktop.
Singly Linked List implementation in JavaScript using ES2015 syntax
class Node{
constructor(val){
this.val = val;
this.next = null
}
}
class LinkedList{
constructor(valOfHead){
this.head = new Node(valOfHead);
this.length = 1;
}
exist(val){
return !!this.find(val);
}
find(val){
let currentNode = this.head;
while(currentNode != null){
if(currentNode.val == val){
return currentNode;
}
currentNode = currentNode.next;
}
return false;
}
insert(newElement, afterElement){
let newNode = new Node(newElement);
let afterNode = this.find(afterElement);
if(afterNode == false){console.log(`Element ${afterElement} doesn't exist`);return false;}
this.length += 1;
newNode.next = afterNode.next;
afterNode.next = newNode;
}
findPreviousNode(val){
if(!this.exist(val)){console.log(`Element ${val} doesn't exist`);return false;}
let current = this.head;
while(current.next && current.next.val != val){
current = current.next;
}
return current;
}
remove(val){
//Element doesn't exist
if(!this.exist(val)){console.log(`Element ${val} doesn't exist`);return false};
let removed = this.find(val);
//Element we want to remove is the first elment in the list
if(this.head.val == val){
this.head = this.head.next;
}
//element exists But it's not the head element so we will remove it using it's previous node.
else{
let prevNode = this.findPreviousNode(val);
prevNode.next = removed.next;
}
this.length -= 1;
return true;
}
existWithIndex(index){return index < this.length && index >= 0;}
findByIndex(index){
if(!this.existWithIndex(index)){console.log(`No node with Index ${index}`);return false;}
let currentNode = this.head;
let init = 0;
while(currentNode && init < index){
currentNode = currentNode.next;
init += 1;
}
return currentNode;
}
insertWithIndex(index,item){
let newNode = new Node(item);
//Element doesn't exist
if( !this.existWithIndex(index) ){ console.log(`Element with index ${index} doesn't exist`); return false;}
//Insert in the first position
if(index == 0){
newNode.next = this.head;
this.head = newNode;
}else{
//Insert between two nodes
let currentNode = this.findByIndex(index);
let prevNode = this.findPreviousNode(currentNode.val);
newNode.next = currentNode;
prevNode.next = newNode;
}
this.length += 1;
return true;
}
removeWithIndex(index){
// if element doesn't exist
if(!this.existWithIndex(index)){console.log(`No element with index ${index} to be removed`);return false;}
// if it's the firts element in the list
if(index == 0){this.head = this.head.next;}
else{
let removed = this.findByIndex(index);
let prevNode = this.findPreviousNode(removed.val);
prevNode.next = removed.next;
}
this.length -= 1;
return true;
}
display(){
let current = this.head;
let elements = ""
while(current){
elements += `${current.val} ->`;
current = current.next;
}
console.log(elements.substring(0,elements.length - 2) + ".");
}
}
let linked_list = new LikedList("One");
linked_list.display(); // One.
linked_list.insert("Two", "One");
linked_list.display(); // One->Two.
linked_list.insertWithIndex(0, "Half");
linked_list.display(); // Half->One->Two.
linked_list.exist("Hlaf"); // true
linked_list.removeWithIndex(2); // true
linked_list.dipslay(); // Half->One.
linked_list.remove("One"); // true
linked_list.dipslay(); // Half.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment