Insertion in Singly linked list: Beginning, End & Given location

C Program to insert node at Beginning of linked list

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
    };
struct node *head=NULL;
struct node* createNode(){
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
    return (newNode);
    }
  
void insertNodeAtBegin(){
 struct node *newNode, *temp;
 newNode=createNode();
 printf("Enter an element you want to insert at begining of the list:");
 scanf("%d",&newNode->data);
 if(head==NULL){
    head=newNode;
 }
 else{
    temp=head;
    head=newNode;
    head->next=temp;
}
}
void viewList(){
    struct node* temp=head;
    if(temp==NULL){
        printf("list is empty");
    }
    else{
        printf("Elements of Linked List: ");
        while(temp->next!=NULL)
        {
            printf("%d\t",temp->data);
            temp=temp->next;
        }
        printf("%d \t",temp->data);
    }
}
int menu(){
    int choice;
    printf("\n 1. Insert at beginning of Linked List");
    printf("\n 2. Travesre/View Linked List");
    printf("\n 3. Exit");
    printf("\n Please enter your choice: \t");
    scanf("%d",&choice);
    return(choice);
}
 void main(){
    while(1){
        switch(menu()){
            case 1:
                insertNodeAtBegin();
                break;
            case 2:
                viewList();
                break;
            case 3:
                exit(0);
            default:
                printf("invalid choice");   
        }
        getch();
    }
  
}

Output


 1. Insert at beginning of Linked List
 2. Travesre/View Linked List
 3. Exit
 Please enter your choice:      1
Enter an element you want to insert at begining of the list:5

 1. Insert at beginning of Linked List
 2. Travesre/View Linked List
 3. Exit
 Please enter your choice:      1
Enter an element you want to insert at begining of the list:3

 1. Insert at beginning of Linked List
 2. Travesre/View Linked List
 3. Exit
 Please enter your choice:      1
Enter an element you want to insert at begining of the list:6

 1. Insert at beginning of Linked List
 2. Travesre/View Linked List
 3. Exit
 Please enter your choice:      2
Elements of Linked: 6       3       5 

C Program to insert a node at end/tail of linked list

#include<stdio.h> 
#include<conio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
struct node *head=NULL;
struct node* createNode(){
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
    return (newNode);
}
 
void insertNodeAtEnd(){
    struct node *temp,*ptr;
    temp=createNode();
    printf("Enter an element you want to insert at end of list:");
    scanf("%d",&temp->data);
    temp->next=NULL;
    if(head==NULL)
        head=temp;
    else{
        ptr=head;
        while(ptr->next!=NULL){
            ptr=ptr->next;
        }
        ptr->next=temp;
    }
}
void viewList(){
    struct node* temp=head;
    if(temp==NULL){
        printf("list is empty");
    }
    else{
        printf("Elements of Linked List: ");
        while(temp->next!=NULL)
        {
            printf("%d\t",temp->data);
            temp=temp->next;
        }
        printf("%d \t",temp->data);
    }
}
int menu(){
    int choice;
    printf("\n 1.Insert at end of Linked List");
    printf("\n 2.Travesre/View Linked List");
    printf("\n 3.Exit");
    printf("\n Please enter your choice: \t");
    scanf("%d",&choice);
    return(choice);
}
 void main(){
    while(1){
        switch(menu()){
            case 1:
                insertNodeAtEnd();
                break;
            case 2:
                viewList();
                break;
            case 3:
                exit(0);
            default:
                printf("invalid choice");
        }
        getch();
    }
 
}

Output:

1.Insert at end of Linked List
 2.Travesre/View Linked List
 3.Exit
 Please enter your choice:      1
Enter an element you want to insert at end of list:5

 1.Insert at end of Linked List
 2.Travesre/View Linked List
 3.Exit
 Please enter your choice:      1
Enter an element you want to insert at end of list:6

 1.Insert at end of Linked List
 2.Travesre/View Linked List
 3.Exit
 Please enter your choice:      1
Enter an element you want to insert at end of list:3

 1.Insert at end of Linked List
 2.Travesre/View Linked List
 3.Exit
 Please enter your choice:      2
Elements of Linked List: 5      6       3 

C Program to insert a node at a given location of linked list

#include<stdio.h> 
#include<conio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
struct node *head=NULL;
struct node* createNode(){
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
    return (newNode);
}
 
void insertNodeAtEnd(){
    struct node *temp,*ptr;
    temp=createNode();
    printf("Enter an element you want to insert:");
    scanf("%d",&temp->data);
    temp->next=NULL;
    if(head==NULL)
        head=temp;
    else{
        ptr=head;
        while(ptr->next!=NULL){
            ptr=ptr->next;
        }
        ptr->next=temp;
    }
}

void insertNodeAtLocation(){
    int loc,i;
    struct node *temp,*ptr, *tempprev, *tempnext;
    temp=createNode();
    printf("Enter an element you want to insert in list:");
    scanf("%d",&temp->data);
    printf("Enter the the location where you want to insert in list:");
    scanf("%d",&loc);
    temp->next=NULL;
    if(loc<1 && head==NULL){
        printf("Invalid location");
    }else{
        ptr=head;
        for(i=0; i<loc-1; i++){
            tempprev=ptr;
            ptr=ptr->next;
            
        }
        tempnext=ptr;
        tempprev->next=temp;
        temp->next=tempnext;    
        
    }
}

void viewList(){
    struct node* temp=head;
    printf("Elements of Linked: ");
    if(temp==NULL){
        printf("list is empty");
    }
    else{
        while(temp->next!=NULL)
        {
            printf("%d\t",temp->data);
            temp=temp->next;
        }
        printf("%d \t",temp->data);
    }
}
int menu(){
    int choice;
    printf("\n 1.Insert at end of Linked List");
    printf("\n 2.Travesre/View List");
    printf("\n 3.Add element at given location of Linked List");
    printf("\n 4.exit");
    printf("\n Please enter your choice: \t");
    scanf("%d",&choice);
    return(choice);
}
 void main(){
     printf("Note: First Insert some value in Linked List before inserting at location");
    while(1){
        switch(menu()){
            case 1:
                insertNodeAtEnd();
                break;
            case 2:
                viewList();
                break;
            case 3:
                insertNodeAtLocation();
                break;
            case 4:
                exit(0);
            default:
                printf("invalid choice");
        }
        getch();
    }
 
}

Output

Note: First Insert some value in Linked List before inserting at location
 1.Insert at end of Linked List
 2.Travesre/View List
 3.Add element at given location of Linked List
 4.exit
 Please enter your choice:      1
Enter an element you want to insert:4

 1.Insert at end of Linked List
 2.Travesre/View List
 3.Add element at given location of Linked List
 4.exit
 Please enter your choice:      1
Enter an element you want to insert:2

 1.Insert at end of Linked List
 2.Travesre/View List
 3.Add element at given location of Linked List
 4.exit
 Please enter your choice:      1
Enter an element you want to insert:7

 1.Insert at end of Linked List
 2.Travesre/View List
 3.Add element at given location of Linked List
 4.exit
 Please enter your choice:      3
Enter an element you want to insert in list:9
Enter the the location where you want to insert in list:2

 1.Insert at end of Linked List
 2.Travesre/View List
 3.Add element at given location of Linked List
 4.exit
 Please enter your choice:      2
Elements of Linked: 4   9       2       7 
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now