DEV Community

Cover image for Linked List Data Structure Using Javascript
Nikhil Bobade
Nikhil Bobade

Posted on

Linked List Data Structure Using Javascript

Linked List

A Linked List is a linear data structure. The Linked List consisting of a group of nodes that together represent a sequence.

Linked List has behaved like each node contains a connection to another link or another node. also linked list is the most used data structure after the array.
1.Insert
2.Delete
3.Search
4.Null

Insert :

The insert method is used for adding the data to the LinkedList.

Delete :

The delete is deleting the given node or element from the linked list.

Search :

A search returns a given node on the linked list.

Null :

if the next element is not having a data or node then this condition was true and return null.

Alt Text

This is how a linked list works the 1st is head and after I go for the next node or data if he doesn't get the next data then return null.

LinkedList example using Javascript :

class LinkedListNode{
    constructor(data){
        this.data = data;
        this.next = null;
        this.previous = null;
    }
}

class LinkedList{
    constructor(){
        this.head = null;
        this.tail = null;
        this.length = null;
    }

    insert(data){
        const node = new LinkedListNode(data);

        if(!this.head){
            this.head = node;
            this.tail = node;
        }
        else{
            this.tail.previous = node;
            this.tail.next = node;
            this.tail = node;
        }

        this.length +=1;
    }
}

const dataList = new LinkedList();

dataList.insert(10);
dataList.insert(34);
dataList.insert(53);
dataList.insert(45);

let currentNode = dataList.head;

for (let i = 0; i < dataList.length; i++) {
    console.log(currentNode.data);
    currentNode = currentNode.next;
}


console.log(JSON.stringify(dataList, null , 2));

Enter fullscreen mode Exit fullscreen mode

Output :

Alt Text

I hope you like this also comments about your thoughts.

For more content follow me on Instagram @developer_nikhil27.

If you want to more support me then buy me a coffee.

Thank you.

Top comments (0)