FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
1.8k views
in Manipal University Jaipur B.Tech(CSE-III Sem) Data Structure Lab by Goeduhub's Expert (7.6k points)
edited by
insertion after specified node in singly linked list

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Insertion after specified node

Algorithm to insert a node  after specified node

STEP 1: IF PTR = NULL

WRITE OVERFLOW
    GOTO STEP 12
   END OF IF

STEP 2: SET NEW_NODE = PTR

STEP 3: NEW_NODE → DATA = VAL

STEP 4: SET TEMP = HEAD

STEP 5: SET I = 0

STEP 6: REPEAT STEP 5 AND 6 UNTIL I

STEP 7: TEMP = TEMP → NEXT

STEP 8: IF TEMP = NULL

WRITE "DESIRED NODE NOT PRESENT"
     GOTO STEP 12
    END OF IF
 END OF LOOP

STEP 9: PTR → NEXT = TEMP → NEXT

STEP 10: TEMP → NEXT = PTR

STEP 11: SET PTR = NEW_NODE

STEP 12: EXIT

Example

image

Program

#include <stdio.h>

#include<conio.h>

#include<stdlib.h>  

void insert(int);  

void create(int);  

struct node  

{  

    int data;  

    struct node *next;  

};  

struct node *head;  

void create(int item)  

{        

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

        if(ptr == NULL)  

        {  

            printf("\nOVERFLOW\n");  

        }  

        else  

        {  

            ptr->data = item;  

            ptr->next = head;  

            head = ptr;  

            printf("\nNode inserted\n");  

        }  

}  

void randominsert(int item)  

    {  

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

        struct node *temp;  

        int i,loc;  

        if(ptr == NULL)  

        {  

            printf("\nOVERFLOW");  

        }  

        else  

        {                

            printf("Enter the location");  

            scanf("%d",&loc);             

            ptr->data = item;  

            temp=head;  

            for(i=0;i<loc;i++)  

            {  

                temp = temp->next;  

                if(temp == NULL)  

                {  

                    printf("\ncan't insert\n");  

                    return;  

                }                

            }  

            ptr ->next = temp ->next;   

            temp ->next = ptr;   

            printf("\nNode inserted");  

        }            

    } 

void display()  

{  

    struct node *ptr;  

    ptr = head;   

    if(ptr == NULL)  

    {  

        printf("Nothing to print"); 

    } 

    else   {  

        printf("\nvalues are\n");   

        while (ptr!=NULL)  

        {  

            printf("\n%d",ptr->data);  

            ptr = ptr -> next;  

        }     }  }

void main ()  

{  

    int item,loc;  

        printf("\nEnter the item which you want to insert : ");  

        scanf("%d",&item);  

        if(head == NULL)  

        {  

            create(item);  

        }  

        else  

        {  

            insert(item);        

        } 

        display();

}  

Output

imageoutput


For more Manipal University Jaipur B.Tech CSE-III Sem Data Structure Lab Experiments Click here


Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...