Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Insertion at the Beginning of a Singly Linked List in C++

Last Updated on May 9, 2023 by Prepbytes

One of the operations that we may carry out on a Linked List in C++ is Insertion at Beginning. Node and pointer are the two components that make up linked, where the node holds the data and the pointer holds the address of the subsequent node.

What is a Singly Linked List?

A linked list is a linear data structure with several nodes that are linked to one another. Each node has two fields: the data field and the following node’s address. So, in this article, we’ll look at how to implement insertion at the start of a singly linked list in C++. Here is a template for a singly linked list Node that we create.

Syntax of Linked List

class Node  
{   
    int data;  
    Node *next;  
};

How to Insert at the Beginning of a Singly Linked List in C++

Assume you’ve been given a single linked list.The goal is to place a node at the start of the given linked list.

For example,
Input :

10 βˆ’ 20 β†’ 30 β†’ 40 β†’ 50
Insert β€˜0’ at the head or beginning of the given linked list.

Output :

0 β†’ 10 β†’ 20 β†’ 30 β†’ 40β†’ 50

It will print the linked list after inserting the node at the beginning of it as
0 β†’ 10 β†’ 20 β†’ 30 β†’ 40β†’ 50

Algorithm for Insertion at the Beginning of a Singly Linked List

Below mentioned steps can be followed for insertion at the beginning of a singly linked list.

  1. Make a new node using the given data.
  2. If the linked list’s head is null, set the new node as the linked list’s head and return.
  3. Set the new node’s next pointer to the current head of the linked list.
  4. Set the linked list’s head to point to the new node.
  5. Return.

Pseudo Code

function insertAtBeginning(data):
    newNode = Node(data)
    if head is null:
        head = newNode
        return
    newNode.next = head
    head = newNode
    return

Now let us discuss the code implementation of insertion at the beginning of a singly linked list in C++.

Code Implementation in C++

#include <iostream>
struct Node {
    int data;
    Node* next;
};

void insertAtBeginning(Node*& head, int data) {
    Node* newNode = new Node{data, head};
    head = newNode;
}

int main() {
    Node* head = NULL; // initialize an empty linked list
    insertAtBeginning(head, 10); // insert a node with data 10
    insertAtBeginning(head, 20); // insert a node with data 20
    insertAtBeginning(head, 30); // insert a node with data 30

    // print the linked list
    for (Node* curr = head; curr != NULL; curr = curr->next) {
        std::cout << curr->data << " ";
    }
    // expected output: 30 20 10 

    return 0;
}

Output :

30 20 10 

Conclusion
In this article, we have discussed how we can perform insertion at the beginning of a singly linked list in C++, along with the steps and proper pseudocode. The code implementation has also been discussed in this article.

Frequently Asked Questions (FAQs)

Q1. How to perform insertion at the beginning in a Singly Linked List in C++?
Ans.

  • Make memory for the new Node.
  • Assign a value to the data.
  • Assign the current head to the newNode next.
  • Replace the head with this newNode.

Q2. What is insertion and deletion operation in singly linked list?
Ans. Insertion inserts an element at the start of the list. Deletion removes an element from the list’s beginning.

Q3. What are the ways to insert a node in a linked list?
Ans.

  • Add Items to a Linked List
  • Insert at the start. Make RAM available for the new node. Save information. Change the node after that to point to the head.
  • Add at the end. Make RAM available for the new node. Save information. Return to the last node.
  • Insert in the center.

Q4. Why are insertion and deletion faster in a linked list?
Ans. Because less memory needs to be manipulated, insertion and deletion are faster in a linked list.

Q5. How do you remove a node from the beginning of a linked list?
Ans. deleteFromStart() will remove a node from the list’s beginning: If the head is null (an empty list), it displays the message "List is empty" and returns. If the list is not empty, it will check to see if there is just one node in the list.

Leave a Reply

Your email address will not be published. Required fields are marked *