How To Create A Linked List In Javascript | letsbug

     Hi in this article we are learning how we can implement linked-list in javascript. We have already seen how we can implement stack data structure using linked lists and we are looking at the linked list themselves.

    Linked list is a linear data structure which is more flexible than array. That is what i have to say about on defining them. You can learn more about them online we will just focus on implementing them in javascript. And perform operations on them.

    List of operation on linked list that we will implement are:

  1. insert() 
  2. append()
  3. print()
  4. length()
  5. deleteLast()
  6. deleteFirst()
  7. findAt()

    We are implementing the singly linked list in which every node is linked with a next node. Which means node store the address of the node which will come next to it. But does not know or store from where is has come from or its previous node.

Linked List In Javascript


Code: 


// Constructor Class for Ndoes
class Node {
    constructor(value, next = null) {
        this.value = value
        this.next = next
    }
}
// Wrapper Linked list class
class LinkedList {
    constructor() {
        this.head = null
        this.size = 0
    }
    //insert data to the start of the linkedList
    insert(data) {
        let current = this.head
        this.head = new Node(data, current)
        this.size++
    }
    //insert data to the end of the linkedList
    append(data) {
        let current = this.head
        let previous = current
        while (current) {
            previous = current
            current = current.next
        }
        previous.next = new Node(data)
        this.size++
    }
    //print the linkedList
    print() {
        let current = this.head
        while (current) {
            console.log(current.value)
            current = current.next
        }
    }
    // delete the last node in the linkedList
    deleteLast() {
        let current = this.head
        let previous = current
        while (current.next) {
            previous = current
            current = current.next
        }
        previous.next = null
        this.size--
    }
    // delete the first node in the linkedList
    deleteFirst() {
        this.head = this.head.next
        this.size--
    }
    // return the length of the linkedList
    length() {
        return this.size
    }

}

    About we have create the main classes for the Linked list and have also completed all required methods. Now lets initialize this linked list.


const l = new LinkedList()
for (let i = 0; i < 10; i++) {
    l.insert(i) // 0 1 2 3 4 5 6 7 8 9
}
console.log("Print the linkedList")
l.print() // 0 1 2 3 4 5 6 7 8 9
l.deleteLast() // 0 1 2 3 4 5 6 7 8
l.deleteFirst() // 1 2 3 4 5 6 7 8
console.log("Print the linkedList After deleting last and first node")
l.print() // 1 2 3 4 5 6 7 8 10
l.append(10) // 1 2 3 4 5 6 7 8 10
console.log(l.length()) //9
</ >

Output:

$ node linkedList.js
Print the linkedList
9
8
7
6
5
4
3
2
1
0
Print the linkedList After deleting last and first node
8
7
6
5
4
3
2
1
9

     And that is how you can implement linked list in javascript. If you want to see the linked list representation of Stack then click here and how to make stack with array click here.
Thank you ☺  

Comments

Categories

Big Data Analytics Binary Search Binary Search Tree Binary To Decimal binary tree Breadth First Search Bubble sort C Programming c++ Chemical Reaction and equation class 10 class 10th Class 9 Climate Complex Numbers computer network counting sort CSS Cyber Offenses Cyber Security Cyberstalking Data Science Data Structures Decimal To Binary Development diamond pattern Digital Marketing dust of snow Economics Economics Lesson 4 Email Validation English fire and ice Food Security in India Footprints Without feet Forest And Wildlife Resources game Geography Geography lesson 6 glassmorphism Glossary Graph HackerRank Solution hindi HTML image previewer India-Size And Location Insertion Sort Internet Network Status Interview Questions Introduction to cyber crime and cyber security IT javascript tricks json to CSV converter lesson 2 lesson 1 lesson 2 Lesson 3 Lesson 6 lesson 7 Life lines of National Economy life processes Linear Search Linked List lowest common ancestor Machine Learning MCQs median in array Merge sort min and max of two numbers Moment Money and Credit My Childhood Natural Vegetation and Wildlife NCERT Network connectivity devices Network Models Network Security No Men Are foreign Node.js operator overloading P5.js PHP Physical features of India Population Prime Numbers python Quick sort R language Rain on the roof Regular Expression Resources and development reversing array saakhi science Searching Algorithm Selection sort Social Media Marketing social science Software Engineering Software Testing Sorting Algorithm Stacks staircase pattern System Concepts Text Recognition The last Leaf time converter Time Passed From A Date Todo List App Tree Trending Technologies Understanding Economic Development username and password video player Visualization water resources Wired And Wireless LAN साखी
Show more

Popular Posts

Big Data MCQs(multiple choice questions) with answers - letsbug

Digital Marketing MCQ(Multiple Choice Questions) with Answers | part 1 | letsbug

Software Engineering MCQs questions with answers - letsbug