Insertion at Beginning in Singly Linked List in C++

Insertion at begin in Singly linked list in C++ programming

How to perform insertion at beginning in Singly Linked List in C++?

Insertion at Beginning in Singly Linked List in C++, is one of the operations that we can perform on Linked List. Singly linked list in C++ are part of linked list and is a type of linear data structure. Linked is made up of two parts node and pointer where node contains the data and pointer contains the address of the next node.

To add a new node add beginning we need to allocate space for the new node first, then we have to store the address of previous head in the pointed of newly created node and make the new node as head of the linked list. In this way insertion at beginning is done in linked list.

Singly linked list definition in C++

Nodes of singly linked list is created by using the code mentioned besides.
This set of code will construct linked list by creating each node of the list.

class Node  

    int data; 
    Node *next; 
}; 

Steps to insert an element at beginning of singly linked list

 1.) Allocate space for a new node.

    • ptr = (struct node *) malloc(sizeof(struct node *));

  2.) In the space created for new node put the data in.

    • new_node->data = new_data

 3.) Point the pointer of new node to the head of singly linked list

    • new_node->ptr = (*head);

4.) Make new node as head.

    • (*head) = new_node
Insertin at beginning in singly linked list in C++

Algorithm to insert an element at beginning of singly linked list

  • IF PTR = NULL
  • EXIT
  • ELSE SET NEW_NODE = PTR
  • SET PTR = PTR → NEXT
  • SET NEW_NODE → DATA = VALUE
  • SET NEW_NODE → NEXT = HEAD
  • SET HEAD = NEW_NODE
  • EXIT

C++ program to insert an element at beginning of singly linked list

#include <iostream>
using namespace std;

struct node 
{
    int num;                
    node *nextptr;             
}*stnode; //node constructed

void createList(int n);                 
void insertatBegin(int num);	            
void display();                          
 
int main()
{
    int n,num;
		
    cout<<"Enter the number of nodes: ";
    cin>>n;
    createList(n);
    cout<<"\nLinked list data: \n";		
    display();
    cout<<"\nEnter data you want to insert at the beginning: ";
    cin>>num;
    insertatBegin(num);
    cout<<"\nLinked list after insertion: \n";		
    display();

    return 0;
}
void createList(int n) //function to create linked list.
{
    struct node *frntNode, *tmp;
    int num, i;
 
    stnode = (struct node *)malloc(sizeof(struct node));
    if(stnode == NULL)        
    {
        cout<<" Memory can not be allocated";
    }
    else
    {
                                  
        cout<<"Enter the data for node 1: ";
        cin>>num;
        stnode-> num = num;      
        stnode-> nextptr = NULL; //Links the address field to NULL
        tmp = stnode;
 
        for(i=2; i<=n; i++)
        {
            frntNode = (struct node *)malloc(sizeof(struct node)); 
 

            if(frntNode == NULL) //If frntnode is null no memory cannot be alloted
            {
                cout<<" Memory can not be allocated";
                break;
            }
            else
            {
                cout<<"Enter the data for node "<<i<<": "; // Entering data in nodes.
                cin>>num;
                frntNode->num = num;         
                frntNode->nextptr = NULL;    
                tmp->nextptr = frntNode;     
                tmp = tmp->nextptr;
            }
        }
    }
} 

void insertatBegin(int num) //function to insert element at beginning
{
    struct node *frntNode;
    frntNode = (struct node*)malloc(sizeof(struct node));
    if(frntNode == NULL)
    {
        cout<<" Memory can not be allocated";
    }
    else
    {
        frntNode->num = num;         //Linkinking data
        frntNode->nextptr = stnode; //Linking address
        stnode = frntNode; 
    }
}

void display() //function to print linked list
{
    struct node *tmp;
    if(stnode == NULL)
    {
        cout<<" No data found in the list";
    }
    else
    {
        tmp = stnode;
        cout<<"Linked List: ";
        while(tmp != NULL)
        {
            cout<<"\t"<<tmp->num;         
            tmp = tmp->nextptr;         
        }
    }
} 
Output:
Enter the number of nodes: 4
Enter the data for node 1: 11
Enter the data for node 2: 22
Enter the data for node 3: 33
Enter the data for node 4: 44

Linked list data: 
Linked List: 	11	22	33	44
Enter data you want to insert at the beginning: 55

Linked list after insertion: 
Linked List: 	55	11	22	33	44

Learn more about Linked List!