Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1264

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T06:31:07+00:00 2024-11-25T06:31:07+00:00

🔗Build A Linked List In Javascript With All Operations

  • 62k

In this post , I will be showing how we can build a singly linked list data structure using javascript language.We will also learn all operations on it.

🧐Overview

In a singly linked list each item points to its successor.Each item in a linked list is called as a node.There is a head node in each linked list which points to its first node.The last node of linked list points to null.

Node Class

First we will create a blue print for our node of linked list.We will create a Node Class in javascript which will have two properties-

  • data – which will store data of node
  • next – which will points to next node
class Node {     constructor(data, next = null) {         this.data = data;         this.next = next;     } }  
Enter fullscreen mode Exit fullscreen mode

Linked List Class

Now we will create a actual linked list class which will have two properties –

  • head -which will points to the first node of linked list
  • size -which represents the number of nodes present in the list
class LinkedList {     constructor() {         this.head = null;         this.size = 0;     } }  
Enter fullscreen mode Exit fullscreen mode

Operations

Now , we will create some member functions in LinkedList class to perform various operations on linked list.

(1) insertFirst()

To insert a node at first position of list.
Here we will simply point the head to new node and next of new node to previous head node . At the end , we will increment the size of list by 1.

    // Insert Node at first position     insertFirst(data) {         this.head = new Node(data, this.head);         this.size++;     } 
Enter fullscreen mode Exit fullscreen mode

(2) insertLast()

To insert a node at last position of list.
Here we will have 2 case
1.If list is empty – then we will simply point the head of node to new node.
2.if list is not empty – then we will traverse the whole list and then point the next of last node to new node.

    // Insert Node at last position     insertLast(data) {         const newLast = new Node(data);          // Check if list is empty then last node is first node which will be head         if (!this.head) {             this.head = newLast;         }         else {             // if list is not empty traverse to last node             let last = this.head;             while (last.next) {                 last = last.next;             }             last.next = newLast;         }         this.size++;     } 
Enter fullscreen mode Exit fullscreen mode

(3) insertAt()

To insert a node at given index.
Here also we will have 2 case
1.If index is 0 – then we will simply point the head of node to new node.
2.if index is not 0 – then we will create two variables to track previous and current node and will traverse the list upto given index and and then point the next of previous node to new node and next of new node to current node.

// insert a node at a particular index     insertAt(data, index) {         // check if index is valid         if (index >= 0 && index < this.size) {             // if first index             if (index === 0) {                 this.head = new Node(data, this.head);                 return;             }              const node = new Node(data);             let current, previous;              current = this.head;             let count = 0;              while (count < index) {                 previous = current;                 count++;                 current = current.next;             }              node.next = current;             previous.next = node;              this.size++;         }         else {             console.log('You have entered an invalid index!!');         }     }  
Enter fullscreen mode Exit fullscreen mode

And that's it for this one. To see how we can write remaining four operations which are
4.removeAt() – To remove a node at given index.
5.getAt() – To get data of a node at given index.
6.clearList() – To clear (empty) a whole list.
7.printListData() – To print data of all nodes in list
visit satishnaikawadi.me

I hope 😇 all of you understand how to build linked list with javascript. Thank you for reading.For any queries , feel free to ask.

beginnersjavascriptnodewebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.