Insertion of Node in Linked List C Program | codeitwise

Here we are going to write a c program for insertion of node in linked list or singly linked list. There are various conditions of insertion of node in linked list viz. insertion at the beginning, insertion at the end, insertion after a given node, insertion before a given node and insertion at a given node. We will stick to basic insertion operations in this post i.e., insertion at the beginning and insertion at the end.

Before we proceed, if you don't have basic knowledge of linked list or want to learn everything about singly linked list at one go then you must visit singly linked list post which is a part of our dsa series and also types of linked list series. 


1. Inserting a node at the beginning of a singly linked list- We first allocate memory space for new node. Set its DATA part with the value and the NEXT part is initialized with the address of the first node stored in HEAD. Since we are adding the new node at the beginning i.e., it will be our first node. So now we will point HEAD towards this new node that is HEAD will store the address of this new node.


insertion at the beginning of linked list


C program to insert node at the beginning of singly linked list -


#include <stdio.h>
#include <malloc.h>

struct node
{
    int data;
    struct node* next;
};

void insertatbeg(struct node** head)
{
    int n;
    printf("Enter value to insert at beginning: ");
    scanf("%d",&n);
    struct node* val=(struct node*)malloc(sizeof(struct node));
    val->data=n;
    val->next=*head;
    *head=val;

}
void display(struct node* head)
{
    struct node* t=head;
    while(t!=NULL)
    {
        printf("%d => ",t->data);
        t=t->next;
    }
    printf("NULL \n");
    
}

struct node* head=NULL;
void main()
{
    insertatbeg(&head);
    display(head);
}

Output -
Enter value to insert at beginning: 2
2 => NULL


2. Inserting a node at the end of singly linked list- We first allocate memory space for new node. Set its DATA part with the value and the NEXT part is initialized with NULL. Then we will use a pointer variable t and initialize it with HEAD i.e., t will point to the first node. In while loop we will traverse through the linked list to reach the last node. Once reached, we will change last node's NEXT pointer to store the address of the new node and point the NEXT pointer of new node to NULL. 

insertion at the end

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


#include <stdio.h>
#include <malloc.h>

struct node
{
    int data;
    struct node* next;
};

void insertatend(struct node** head)
{
    int n;
    printf("Enter value to insert at end: ");
    scanf("%d",&n);
    struct node* val=(struct node*)malloc(sizeof(struct node));
    val->data=n;
    val->next=NULL;
    if(*head==NULL)
    {
        *head=val;
        return;
    }
    struct node* t=*head;
    while(t->next!=NULL)
    {
        t=t->next;
    }
    t->next=val;
}
void display(struct node* head)
{
    struct node* t=head;
    while(t!=NULL)
    {
        printf("%d => ",t->data);
        t=t->next;
    }
    printf("NULL \n");
    
}

int main()
{
    struct node* head=NULL;
    insertatend(&head);
    insertatend(&head);
    insertatend(&head);
    display(head);
    return 0;
}

Output-
Enter value to insert at end: 1
Enter value to insert at end: 2
Enter value to insert at end: 3
1 => 2 => 3 => NULL 



Now let us write a c program to implement both the operations that we just read to get a clear idea about them i.e., c program to insert a new node at the beginning and at the end of a singly linked list.

C program to insert a node at the beginning and at the end of a singly linked list -


#include <stdio.h>
#include <malloc.h>

struct node
{
    int data;
    struct node* next;
};

void insertatbeg(struct node** head)
{
    int n;
    printf("Enter value to insert at beginning: ");
    scanf("%d",&n);
    struct node* val=(struct node*)malloc(sizeof(struct node));
    val->data=n;
    val->next=*head;
    *head=val;

}

void insertatend(struct node** head)
{
    int n;
    printf("Enter value to insert at end: ");
    scanf("%d",&n);
    struct node* val=(struct node*)malloc(sizeof(struct node));
    val->data=n;
    val->next=NULL;
    if(*head==NULL)
    {
        *head=val;
        return;
    }
    struct node* t=*head;
    while(t->next!=NULL)
    {
        t=t->next;
    }
    t->next=val;
}
void display(struct node* head)
{
    struct node* t=head;
    while(t!=NULL)
    {
        printf("%d => ",t->data);
        t=t->next;
    }
    printf("NULL \n");
    
}

struct node* head=NULL;
void main()
{
    int c=0;
    do
    {
        printf("       || Menu ||       \n");
        printf("Press 1 to insert at beginning \n");    
        printf("Press 2 to insert at end \n");    
        printf("Enter your choice: ");
        printf("\n");    
        scanf("%d",&c);
        switch(c)
        {
            case 1:
                insertatbeg(&head);
                display(head);
                break;
            case 2:
                insertatend(&head);
                display(head);
                break;
            default:
                printf("Invalid Input!!!!!");
                break;
        }
    } while (c!=3);
}

Output -
       || Menu ||       
Press 1 to insert at beginning 
Press 2 to insert at end       
Enter your choice: 
1
Enter value to insert at beginning: 2
2 => NULL 
       || Menu ||
Press 1 to insert at beginning
Press 2 to insert at end 
Enter your choice:
2
Enter value to insert at end: 5
2 => 5 => NULL 
       || Menu ||
Press 1 to insert at beginning 
Press 2 to insert at end
Enter your choice: 
1
Enter value to insert at beginning: 1
1 => 2 => 5 => NULL 
       || Menu ||
Press 1 to insert at beginning
Press 2 to insert at end
Enter your choice: 
3
Invalid Input!!!!!

Related Programs-

Post a Comment

If you have any doubts, please let me know

Previous Post Next Post