Advertisement
mhrabbi

Insert Node (At beg,Nth,End) Doubly Linked List

Oct 26th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct node
  5. {
  6.     int data;
  7.     struct node *prev,*next;
  8. }*head,*p;
  9.  
  10. int main()
  11. {
  12.     int n,m,o,position,ndata;
  13.     printf("Enter Total Number Of Node: ");
  14.     scanf("%d",&n);
  15.     createList(n);
  16.  
  17.     printf("\nDoubly Linked List is: ");
  18.     displayList();
  19.  
  20.     printf("\nEnter data to insert at the beginning: ");
  21.     scanf("%d",&m);
  22.  
  23.     insertatbeg(m);
  24.  
  25.     displayList();
  26.  
  27.     printf("\nEnter data to insert at the end: ");
  28.     scanf("%d",&o);
  29.  
  30.     insertatend(o);
  31.  
  32.     displayList();
  33.  
  34.     printf("\nEnter any middle position to add node: ");
  35.     scanf("%d",&position);
  36.  
  37.     printf("\nEnter the data for node %d: ",position);
  38.     scanf("%d",&ndata);
  39.  
  40.     insertATn(ndata,position);
  41.  
  42.     displayList();
  43.  
  44.     return 0;
  45.  
  46. }
  47. void createList(int n)
  48. {
  49.     int i,data;
  50.     struct node *temp;
  51.  
  52.     temp=(struct node *)malloc(sizeof(struct node));
  53.  
  54.     printf("Enter data for node 1: ");
  55.     scanf("%d",&data);
  56.  
  57.     temp->data=data;
  58.     temp->prev=NULL;
  59.     temp->next=NULL;
  60.  
  61.     head=temp;
  62.     p=temp;
  63.  
  64.     for(i=2;i<=n;i++)
  65.     {
  66.         temp=(struct node *)malloc(sizeof(struct node));
  67.         printf("Enter data for node %d: ",i);
  68.         scanf("%d",&data);
  69.  
  70.         temp->data=data;
  71.         temp->next=NULL;
  72.         temp->prev=p;
  73.         p->next=temp;
  74.  
  75.         p=p->next;
  76.  
  77.     }
  78. }
  79. void displayList()
  80. {
  81.     struct node *temp;
  82.  
  83.     temp=head;
  84.  
  85.     while(temp != NULL)
  86.     {
  87.         printf("\n%d",temp->data);
  88.         temp=temp->next;
  89.     }
  90. }
  91. void insertatbeg(int data)
  92. {
  93.     struct node *temp;
  94.  
  95.     temp=(struct node *)malloc(sizeof(struct node));
  96.  
  97.     temp->data=data;
  98.     temp->next=head;
  99.     temp->prev=NULL;
  100.  
  101.     head->prev=temp;
  102.  
  103.     head=temp;
  104. }
  105. void insertatend(int data)
  106. {
  107.     struct node *temp;
  108.  
  109.     temp=(struct node *)malloc(sizeof(struct node));
  110.     temp->data=data;
  111.     temp->next=NULL;
  112.     temp->prev=p;
  113.  
  114.     p->next=temp;
  115.     p=temp;
  116. }
  117. void insertATn(int data,int position)
  118. {
  119.     struct node *temp,*q;
  120.     int i=1;
  121.     q=head;
  122.  
  123.  
  124.     temp=(struct node *)malloc(sizeof(struct node));
  125.     temp->data=data;
  126.  
  127.     while(i<position-1 && q != NULL)
  128.     {
  129.         q=q->next;
  130.         i++;
  131.     }
  132.  
  133.  
  134.     temp->prev=q;
  135.     temp->next=q->next;
  136.  
  137.     if(q->next != NULL)
  138.     {
  139.         q->next->prev=temp;
  140.         q->next=temp;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement