DEV Community

Aashish Panchal
Aashish Panchal

Posted on

Singly Link List

class Node{

constructor(val){

this.val = val;

this.next = null; 
Enter fullscreen mode Exit fullscreen mode

}
}

class SinglylinkedList{

constructor(){

this.head = null;

this.tail = null;

this.length = 0;
Enter fullscreen mode Exit fullscreen mode

}

// 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;
Enter fullscreen mode Exit fullscreen mode

}

// Show all list

traverse(){

var current = this.head;

while(current){

    console.log(current.val)

    current = current.next;

}
Enter fullscreen mode Exit fullscreen mode

}

// 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;
Enter fullscreen mode Exit fullscreen mode

}

// 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;

}
Enter fullscreen mode Exit fullscreen mode

}

// 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;
Enter fullscreen mode Exit fullscreen mode

}

// 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;
Enter fullscreen mode Exit fullscreen mode

}

// Change the value in the node

set(index, val){

var foundNode = this.get(index);

if(foundNode){

    foundNode.val = val;

    return true;

}

return false;
Enter fullscreen mode Exit fullscreen mode

}

// 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;
Enter fullscreen mode Exit fullscreen mode

}

// 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;
Enter fullscreen mode Exit fullscreen mode

}

// 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
Enter fullscreen mode Exit fullscreen mode

}

// Print all Value

print(){

var arr = [];

var current = this.head

while(current){

   arr.push(current.val)

   current = current.next 

}

console.log(arr);
Enter fullscreen mode Exit fullscreen mode

}
}

var list = new SinglylinkedList()

list.push("Java")

list.push("Java script")

list.push("Html")

list.push("Css")

Top comments (0)