Finance[US] Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
3.0k views
in RTU/BTU B.Tech(CSE-III Sem) DSA LAB by Goeduhub's Expert (5.8k points)
Insertion after Specified node in Doubly Linked List

1 Answer

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

Insertion after Specified node in Doubly Linked List

insertion after specified node in doubly linked list

Algorithm

  1. IF PTR = NULL
  2. SET NEW_NODE = PTR
  3. SET PTR = PTR -> NEXT
  4. SET NEW_NODE -> DATA = VAL
  5. SET TEMP = START
  6. SET I = 0
  7. REPEAT 8 to 10 until I
  8. SET TEMP = TEMP -> NEXT
  9. IF TEMP = NULL
  10. WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
  11. SET NEW_NODE -> NEXT = TEMP -> NEXT
  12. SET NEW_NODE -> PREV = TEMP
  13. SET TEMP -> NEXT = NEW_NODE
  14. SET TEMP -> NEXT -> PREV = NEW_NODE
  15. EXIT

Program Code

#include<stdio.h>  

#include<stdlib.h>  

#include<conio.h>

void insert_specified(int);  

void create(int);  

struct node  

{  

    int data;  

    struct node *next;  

    struct node *prev;  

};  

struct node *head;  

void main ()  

{  

clrscr();

    int choice,item,loc;  

    do   

    {  

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

        scanf("%d",&item);  

        if(head == NULL)  

        {  

            create(item);  

        }  

        else  

        {  

            insert_specified(item);  

        }  

        printf("\nPress 0 to insert more! ");  

        scanf("%d",&choice);  

    }while(choice == 0);  

}  

void create(int item)  

    {  

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

   if(ptr == NULL)  

   {  

       printf("\nOVERFLOW");  

   }  

   else  

   {   

   if(head==NULL)  

   {  

       ptr->next = NULL;  

       ptr->prev=NULL;  

       ptr->data=item;  

       head=ptr;  

   }  

   else   

   {  

       ptr->data=item;printf("\nPress 0 to insert more! ");  

       ptr->prev=NULL;  

       ptr->next = head;  

       head->prev=ptr;  

       head=ptr;  

   }  

    printf("\nNode Inserted!");  

}  

}  

void insert_specified(int item)  

{  

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

   struct node *temp;   

   int i, loc;   

   if(ptr == NULL)  

   {  

       printf("\nOVERFLOW");  

   }  

   else  

   {  

       printf("\nEnter the location: ");  

       scanf("%d",&loc);  

       temp=head;  

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

       {  

           temp = temp->next;  

           if(temp == NULL)  

           {  

               printf("Can't insert!");  

               return;  

           }  

       }  

       ptr->data = item;  

       ptr->next = temp->next;  

       ptr -> prev = temp;  

       temp->next = ptr;  

       temp->next->prev=ptr;  

       printf("Node Inserted!");  

   }  

}  

Output


For more Data Structures & Algorithms Tutorials 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

...