9

Linked List Data Structure Using Javascript

 3 years ago
source link: https://dev.to/nikhil27b/linked-list-data-structure-using-javascript-2n09
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

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 modeExit fullscreen mode

Output :

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK