Singly Link List
Aashish Panchal
Posted on October 15, 2020
class Node{
constructor(val){
this.val = val;
this.next = null;
}
}
class SinglylinkedList{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
// Add a new Value to the list
push(val){
var newNode = new Node(val);
if(!this.head){
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
console.log(` Your ${this.length} added value is --> ${val}`);
return this;
}
// Show all list
traverse(){
var current = this.head;
while(current){
console.log(current.val)
current = current.next;
}
}
// Delete end value in the list
pop(){
if(!this.head) return undefined;
var current = this.head;
var newTail = current;
while(current.next){
newTail = current;
current = current.next;
}
this.tail = newTail;
this.tail.next = null;
this.length--;
if(this.length === 0){
this.head = null;
this.tail = null;
}
console.log(`${current.val} <- This value is deleted`)
console.log(`${newTail.val} <- This is a Next value`)
return current;
}
// Delete Head Value in the list
Shift(){
if(!this.head) return undefined;
var currentHead = this.head;
this.head = currentHead.next;
this.length--;
return currentHead;
if(this.length === 0){
this.tail = null;
}
}
// Add value in the head in the list
unShift(val){
var newNode = new Node(val)
if(!this.head){
this.head = newNode;
this.tail = this.head;
} else {
newNode.next = this.head;
this.head = newNode;
}
this.length++;
return this;
}
// Write a Node number and Show this number Node name
get(index){
if(index < 0 || index >= this.length) return null;
var counter = 0;
var current = this.head;
while(counter !== index){
current = current.next;
counter++;
}
return current;
}
// Change the value in the node
set(index, val){
var foundNode = this.get(index);
if(foundNode){
foundNode.val = val;
return true;
}
return false;
}
// insret Node
insret(index, val){
if(index < 0 || index > this.length) return false;
if(index === this.length) return !! this.push(val);
if(index === 0) return !!this.unShift(val)
var newNode = new Node(val);
var prev = this.get(index -1);
var temp = prev.next;
prev.next = newNode;
newNode.next = temp;
this.length++;
console.log(` You are added no.${this.length} -> The value is -> ${val}`)
return true;
}
// Remove a Node
remove(index){
if(index < 0 || index >= this.length) return undefined;
if(index === 0) return this.Shift()
if(index === this.length -1) return this.pop();
var previousNode = this.get(index -1);
var remove = previousNode.next;
this.length--;
return remove;
}
// Print a Reverse Node Value
reverse(){
var node = this.head;
this.head = this.tail;
this.tail = node
var next;
var prev = null;
for(var i = 0; i < this.length; i++){
next = node.next;
node.next = prev;
prev = node;
node = next;
}
return this
}
// Print all Value
print(){
var arr = [];
var current = this.head
while(current){
arr.push(current.val)
current = current.next
}
console.log(arr);
}
}
var list = new SinglylinkedList()
list.push("Java")
list.push("Java script")
list.push("Html")
list.push("Css")
đź’– đź’Ş đź™… đźš©
Aashish Panchal
Posted on October 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.