How to insert a node in a linked list

0 votes

How to insert a node in a linked list?

I’m learning about linked lists and need help understanding how to insert a node into one. Specifically, I want to know how to handle different cases like inserting a node at the beginning, in the middle, or at the end of the linked list. Could someone explain the process and provide a clear explanation for these scenarios with simple examples?

Dec 6, 2024 in Web Development by Nidhi
• 5,440 points
66 views

1 answer to this question.

0 votes

To insert a node at the front of a linked list, the operation involves creating a new node, linking it to the current head of the list, and updating the head to the newly created node. This process is efficient as it requires minimal adjustments to pointers.

Algorithm

-            Create a new node with the given data.

-            Point the next reference of the new node to the current head of the list.

-            Update the head of the list to this new node.

Implementation in C++

#include <bits/stdc++.h>


using namespace std;

// Definition of the Node structure

struct Node {

    int data;        // Stores the data of the node

    Node* next;      // Pointer to the next node in the list

    // Constructor to initialize the node

    Node(int new_data) {

        data = new_data;

        next = nullptr;

    }

};

// Function to insert a new node at the beginning of the linked list

Node* insertAtFront(Node* head, int new_data) {

    // Create a new node with the given data

    Node* new_node = new Node(new_data);

    // Link the new node to the current head

    new_node->next = head;

    // Return the new node as the new head of the list

    return new_node;

}

// Function to print the elements of the linked list

void printList(Node* head) {

    Node* curr = head; // Start from the head of the list

    // Traverse the list and print the data of each node

    while (curr != nullptr) {

        cout << " " << curr->data;

        curr = curr->next;

    }

    cout << endl; // Newline at the end

}

// Main function

int main() {

    // Creating a linked list with nodes 20 -> 30 -> 40 -> 50

    Node* head = new Node(20);

    head->next = new Node(30);

    head->next->next = new Node(40);

    head->next->next->next = new Node(50);

    // Data for the new node

    int data = 10;

    // Insert the new node at the beginning of the list

    head = insertAtFront(head, data);

    // Print the updated linked list

    printList(head);

    return 0;

}

answered Dec 6, 2024 by Navya

Related Questions In Web Development

0 votes
0 answers

How to upload a file to api server in node js?

How to upload a file to api ...READ MORE

Oct 14, 2024 in Web Development by anonymous
• 5,440 points
112 views
0 votes
0 answers

How to upload a file to api server in node js?

How to upload a file to api ...READ MORE

Oct 21, 2024 in Web Development by Nidhi
• 5,440 points
178 views
0 votes
1 answer

How to read a JSON file into server memory in Node.js?

To read a JSON file into server ...READ MORE

answered Dec 13, 2024 in Web Development by Navya
80 views
0 votes
1 answer

How to set a cookie in Node.js using the Express framework?

You can use the res.cookie() method to ...READ MORE

answered Nov 27, 2024 in Web Development by Navya
79 views
0 votes
1 answer

How to create a node in a linked list?

A linked list is a linear data structure ...READ MORE

answered Nov 6, 2024 in Web Development by kavya
181 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,769 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,459 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,130 points

edited Apr 12, 2018 by nirvana 22,171 views
0 votes
1 answer

How do I send a file from postman to node.js with multer?

npm install multer express Then  we will set ...READ MORE

answered Oct 24, 2024 in Web Development by kavya

edited Oct 30, 2024 by Nidhi 241 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP