Insert a node in the singly linked list

Glory Pachnanda   2019-02-19   Blogger   Data structure > singly-linked-list   1337 Share

Insert a node in a singly linked list

A singly linked list is a dynamic data structure. The singly linked list consists of two fields, data field and link field, link field store the address of the next node. Like the doubly linked list, a singly linked list does not store the address information of the previous node. The singly linked list traversed only in the forward direction. You can see a singly linked list in the image below.

singly linked list and its operation

There are several ways to insert a new node in the singly linked list. The first of which is to add the node at the front of a singly linked list, the other way is to insert the node in the middle and the last method is to add the node to the end of the singly linked list.

Create a node in the singly linked list

struct node
{
int data;
struct node* link;
};
struct node *new_node;
new_node = (struct node*) malloc(sizeof(struct node));

Insert a node at the front of the singly linked list

The following image shows a singly linked list. Let us see how we add a new node to the front of his singly linked list.

singly linked list and its operation

The above images display the linked list and the new node. To add the new node to the linked list, first of all, we need to send the head value in the link field of the new node, after which our new node will connect to the linked list.

singly linked list and its operation

Now our next step is to make the new node as the head of the linked list. To make the new node as a head, the address of the new node must be sent to the head.

singly linked list and its operation

Steps of an algorithm to insert a node at the front of the linked list

1. Create a new_node
2. Check head of the linked list is empty or not
If the head is empty 
then head = new_node (Make new_node as head)
Else
new_node->link= head;
head = new_node;
3. End

C program to insert a node in the singly linked list


#include <stdio.h>
#include <stdlib.h>

//Structure of node
struct node
{
    int data;
    struct node* link;
};

void insert_at_front(struct node** head1, int value); //Declaration of function to insert the node at the front of the linked list
void print(struct node* head); //Declaration of function to print the values of linked list

int main()
{
    struct node* head=NULL; //Declaration and initialization of head
    insert_at_front(&head, 99);  //calling function insert_at_front
    insert_at_front(&head, 63);
    insert_at_front(&head, 29);
    insert_at_front(&head, 80);

    print(head);
return 0;

}

//Function to insert a node at the front
void insert_at_front(struct node** head1,int value)
{
    //Create new node
    struct node* new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = value;
    new_node->link = NULL;


    if ((*head1) ==NULL)       //Check head is empty or not, if yes then move the address of the new node to head.
    {
        (*head1) = new_node;
    }
    else
    {
        new_node->link= (*head1);
        (*head1)=new_node;
    }
}

//Function to print the data values
void print(struct node* head1)
{
    struct node* temp=head1;

    while (temp!= NULL)
    {
        printf(" data=%d \n", temp->data);
        temp = temp->link;
    }
}

 


Output:

singly linked list and its operation

Insert a node at the desired location in the singly linked list

An algorithm to insert a node at the middle of the linked list

code:

1. Create a new node.
2. Check the head is empty or not
If the head is empty then make new node as head
or add a node at the front
3. Calculate the length of the linked list
4. Enter the location in which you want to insert a new node in the linked list and the location of the node should be less than the length of the linked list.
new_node->link = ptr->link;
if (ptr->link!=NULL)
{
ptr->link = new_node;
}
 ptr->link=new_node;
5. End

singly linked list and its operation

C program to insert a node at the desired location in the singly linked list


#include <stdio.h>
#include <stdlib.h>

//Structure of node
struct node
{
    int data;
    struct node* link;
};

void insert_at_front(struct node** head1, int value); //Declaration of function to insert the node at the front of the linked list
void insert_at_middle(struct node** head1, int N, int value);
void print(struct node* head); //Declaration of function to print the values of linked list
int length(struct node* head1); // Declaration of function to calculate length of linked list
int main()
{
    int N;
    struct node* head=NULL; //Declaration and initialization of head
    insert_at_front(&head, 99);  //calling function insert_at_front
    insert_at_front(&head, 63);
    insert_at_front(&head, 29);
    insert_at_front(&head, 80);
    N=length (head);
    insert_at_middle(&head,N,70);
    print(head);
    return 0;

}

//Function to insert a node at the front
void insert_at_front(struct node** head1,int value)
{
    //Create new node
    struct node* new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = value;
    new_node->link = NULL;


    if ((*head1) ==NULL)       //Check head is empty or not, if yes then move the address of the new node to head.
    {
        (*head1) = new_node;
    }
    else
    {
        new_node->link= (*head1);
        (*head1)=new_node;
    }
}

//Function to print the data values
void print(struct node* head1)
{
    //struct node* temp=head1;

    while (head1!= NULL)
    {
        printf(" data=%d \n", head1->data);
        head1 = head1->link;
    }
}

int length(struct node* head1)
{
    int num = 0;
    while (head1!=NULL)
    {
        num++;
        head1=head1->link;
    }
    printf ("length = %d\n", num);
    return num;
}
void insert_at_middle(struct node** head1,int N, int value)
{
    int loc,k=0;

    printf ("Enter Location ");
    scanf ("%d",&loc);

    if (loc>N)
    {
        printf (" Enterd wrong location\n\n");
    }

    else
    {
        // Create a new node
         struct node *new_node, *ptr;
        new_node = (struct node*)malloc (sizeof (struct node));
        new_node->data = value;
        new_node->link= NULL;
        ptr = (*head1);

        while (k<loc)
        {
            ptr=ptr->link;
            k++;
        }

        new_node->link = ptr->link;

        if (ptr->link!=NULL)
        {
        ptr->link = new_node;
        }
        ptr->link=new_node;
    }
}


Output:

singly linked list and its operation

Insert a node at the end of the singly linked list

An algorithm to insert the new node at the end of the singly linked list

1. Create a new node.
2. Traversed the head pointer until NULL
3. head->link = new_node
4. END

singly linked list and its operation

C program to insert a node at the end of the singly linked list.


#include <stdio.h>
#include <stdlib.h>

//Structure of node
struct node
{
    int data;
    struct node* link;
};

void insert_at_front(struct node** head1, int value); //Declaration of function to insert the node at the front of the linked list
void insert_at_end(struct node** head1, int value); //Declaration of function to insert the node at the end of the linked list
void print(struct node* head); //Declaration of function to print the values of linked list

int main()
{
    struct node* head=NULL; //Declaration and initialization of head
    insert_at_front(&head, 99);  //calling function insert_at_front
    insert_at_front(&head, 63);
    insert_at_front(&head, 29);
    insert_at_front(&head, 80);
    insert_at_end(&head,90);

    print(head);
return 0;

}

//Function to insert a node at the front
void insert_at_front(struct node** head1,int value)
{
    //Create new node
    struct node* new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = value;
    new_node->link = NULL;


    if ((*head1) ==NULL)  //Check head is empty or not, if yes then move the address of the new node to head.
    {
        (*head1) = new_node;
    }
    else
    {
        new_node->link= (*head1);
        (*head1)=new_node;
    }
}

//Function to print the data values
void print(struct node* head1)
{
    struct node* temp=head1;

    while (temp!= NULL)
    {
        printf(" data=%d \n", temp->data);
        temp = temp->link;
    }
}

//Function to insert a new node at the end of the linked list
void insert_at_end(struct node** head1, int value)
{
    //Create a new node
    struct node *new_node,*ptr;
    new_node=(struct node*)malloc(sizeof(struct node));
    new_node->data=value; //Insert data 
    new_node->link = NULL;
    ptr = (*head1);
    
    while (ptr->link!=NULL)
    {
        ptr=ptr->link;
    }
    ptr->link=new_node;
}


Output:

singly linked list and its operation
Read about Operator in the C programming language