Doubly Link List

aashish578

Aashish Panchal

Posted on October 15, 2020

Doubly Link List

class Node {

constructor(val) {

this.val = val;

this.next = null;

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

}
}

class DoublylinkList {

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.length === 0) {

    this.head = newNode;

    this.tail = newNode;

} else {

    this.tail.next = newNode;

    newNode.prev = this.tail;

    this.tail = newNode;

}

this.length++;

console.log( ` You are added ${this.length} Node ->> ${val}` );

return this;
Enter fullscreen mode Exit fullscreen mode

}

// πŸ‘‰πŸ‘‰ Delete end value in the Node

pop() {

if(!this.head) return undefined;

var poppedNode = this.tail;

if(this.length === 1) {

    this.head = null;

    this.tail = null;

} else {

    this.tail = poppedNode.prev;

}

this.length--;

console.log(` Deleted Successfully πŸ‘`)

return poppedNode;
Enter fullscreen mode Exit fullscreen mode

}

// πŸ‘‰πŸ‘‰ Delete Head Value in the list

Shift() {

if(!this.head) return undefined;

var oldHead = this.head;

if(this.length === 1) {

    this.head = null;

    this.tail = null;

} else {

    this.head = oldHead.next;

    this.head.prev = null;

    oldHead.next = null;   

}

this.length--;

return oldHead;
Enter fullscreen mode Exit fullscreen mode

}

// πŸ‘‰πŸ‘‰ Add value in the head in the list

unShift(val) {

var newNode = new Node(val);

if(this.length === 0) {

    this.head = newNode;

    this.tail = newNode;

} else {

    this.head.prev = newNode

    newNode.next = this.head;

    this.head = newNode;

}

this.length++;

console.log(" -> Added Successfully πŸ‘" );

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;

if(index <= this.length/2){

    var count = 0;

    var current = this.head;

    while(count != index){

        current = current.next;

        count++;

    }

    return current;

} else {

    var count = this.length - 1;

    var current = this.tail;

    while(count !== index){

        current = current.next;

        count++;

    }

    return current;

}

console.log(" -> Finded Successfully πŸ‘" );
Enter fullscreen mode Exit fullscreen mode

}

// Change the value in the node

set(index, val) {

var foundNode = this.get(index);

if(foundNode != null) {

    foundNode.val = val;

    return true;

}

return false;
Enter fullscreen mode Exit fullscreen mode

}

// insert Node

insert(index, val) {

if(index < 0 || index > this.length) return false;

if(index === 0) return this.unShift(val);

if(index === this.length) return this.push(val);

var newNode = new Node(val);

var beforeNode = this.get(index - 1);

var afterNode = beforeNode.next;

beforeNode.next = newNode;

newNode.prev = beforeNode;

newNode.next = afterNode;

afterNode.prev = newNode;

this.length++;

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 removeNode = this.get(index);

var beforeNode = removeNode.prev;

var afterNode = removeNode.next;

beforeNode.next = afterNode;

afterNode.prev = beforeNode;
Enter fullscreen mode Exit fullscreen mode

// removeNode.prev.next = removeNode.next;

// removeNode.next.next = removeNode.prev;

removeNode.next = null;

removeNode.prev = null;

this.length--;

return removeNode;
Enter fullscreen mode Exit fullscreen mode

}
}

var list = new DoublylinkList()

list.push("Java")

list.push("Java script")

list.push("Html")

list.push("Css")

πŸ’– πŸ’ͺ πŸ™… 🚩
aashish578
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.

Related

Week 13: Release 0.4: Planning
undefined Week 13: Release 0.4: Planning

November 29, 2024

A Final Challenge: The Planning Phase
opensource A Final Challenge: The Planning Phase

November 27, 2024

Going with Mattermost!
opensource Going with Mattermost!

November 25, 2024

Week 12: Release 0.3 pull request 2
undefined Week 12: Release 0.3 pull request 2

November 21, 2024