Quick actions

cmd+k|ctrl+k

Navigation

Languages

Doubly Linked List Implementation

Snippet info

Language

JavaScript

Visibility

public

Author

anikeshdey2020

Created

2022-10-18T06:06:27.639151Z

Updated

2023-02-28T06:30:24.036654Z

class Node{
    constructor(value){
        this.value = value;
        this.next = null;
        this.prev = null; 
    }
}

class LinkedList{
    constructor(value){
        this.head = {
            value:value,
            next: null,
            prev:null
        };
        this.tail = this.head;
        this.length = 1;
    }
    
    prepend(value){
        const head = this.head;
        const node = new Node(value)
        this.head = node; //ref to the node obj in ram not the actual data
        
        this.head.next = head;
        head.prev = node;
        
        this.length++;
    }
    
    
    append(value){
        const newNode = new Node(value)
        //first time this is changing the data location of head h --> {value:10, next: ref{value:5, next:null}} //second time its changing the data in ram and all refs are changing
        this.tail.next = newNode; //this is a ref for newNode in ram not the actual data
        newNode.prev = this.tail;
        //this is a ref for newNode in ram not the actual data so when we change next time its changing the data in ram and all its ref like in head
        this.tail = newNode; 
        this.length++;
        return this;
    }
    
    insert(index, value){
        
        if(index >= this.length){
            return this.append(value);
        }
        
        var newNode = {
            value:value,
            next:null,
            prev:null
        }
        
        var leader = this.traverseToIndex(index -1);
        var nextNode = leader.next;
        leader.next = newNode;
        newNode.prev = leader;
        newNode.next = nextNode;
        nextNode.prev = newNode;
        this.length++;
        return this;
    }
    
    remove(index){
        if(index >= this.length){
            return this
        }
        
        if(index == 0){
            let currentNode = this.head;
            this.head = currentNode.next;
            return this;
        }
        
        var currentNode = this.traverseToIndex(index)
        var previousNode = this.traverseToIndex(index - 1)
        previousNode.next = currentNode.next
        return this.length--
    }
    
    traverseToIndex(index){
        var counter = 0;
        var currentNode = this.head;
        while(index !== counter){
            currentNode = currentNode.next;
            counter++;
        }
        
        return currentNode;
    }
    
    printList(){
        var arr = [];
        var currentNode = this.head;
        while(currentNode !== null){
            arr.push(currentNode.value);
            currentNode = currentNode.next;
        }
        
        return arr;
    }
    
    reverse(){
        let first = this.head;
        this.tail = this.head;
        let second = this.head.next;

        while(second) {
            let temp = second.next;
            first.prev = second;
            second.next = first;
            
            first = second;
            second = temp;
        }

        this.head.next = null;
        this.head = first;
        this.head.prev = null;
        return this;
    }
}


var myLinkedList = new LinkedList(10);
myLinkedList.append(5);
myLinkedList.append(16);
myLinkedList.prepend(35);
myLinkedList.insert(1,99);
//myLinkedList.remove(10)
var arr = myLinkedList.printList();
console.log(myLinkedList);
console.log(arr);
INFO