Advertisement
mhrabbi

Delete a node (atBEg,End,Nth) Doubly Linked List

Oct 26th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Node
  4. {
  5.     int data;
  6.     struct Node *prev;
  7.     struct Node *next;
  8. }*head,*p;
  9. void createlist(int n);
  10. void displayAccending();
  11. void displaydecending();
  12. void Delete();
  13. int main()
  14. {
  15.     int n;
  16.     printf("Enter the number of nodes: ");
  17.     scanf("%d",&n);
  18.     createlist(n);
  19.     printf("Data in the list From 1st to last:\n");
  20.     displayAccending();
  21.     printf("Data in the list from last to first:\n");
  22.     displaydecending();
  23.     Delete();
  24.     printf("Data in the list From 1st to last:\n");
  25.     displayAccending();
  26.     printf("Data in the list from last to first:\n");
  27.     displaydecending();
  28.     return 0;
  29.  
  30. }
  31. void createlist(int n)
  32. {
  33.     struct Node *temp;
  34.     head=NULL;
  35.     p=NULL;
  36.     int data, i;
  37.     head= (struct Node*)malloc(sizeof(struct Node));
  38.     printf("Input Data for node no 1: ");
  39.     scanf("%d",&data);
  40.     head->data=data;
  41.     head->prev=NULL;
  42.     head->next=NULL;
  43.     p=head;
  44.     for(i=2; i<=n; i++)
  45.     {
  46.         temp= (struct Node*)malloc(sizeof(struct Node));
  47.         printf("Input data for node no %d: ",i);
  48.         scanf("%d",&data);
  49.         temp->data=data;
  50.         temp->prev=p;
  51.         temp->next=NULL;
  52.         p->next=temp;
  53.         p=p->next;
  54.  
  55.     }
  56.  
  57. }
  58. void displayAccending()
  59. {
  60.     struct Node *temp;
  61.     temp=head;
  62.     while(temp!=NULL)
  63.     {
  64.         printf("%d\n",temp->data);
  65.         temp=temp->next;
  66.     }
  67. }
  68. void displaydecending()
  69. {
  70.     struct Node *temp;
  71.     temp=p;
  72.     while(temp!=NULL)
  73.     {
  74.         printf("%d\n",temp->data);
  75.         temp=temp->prev;
  76.     }
  77. }
  78.  
  79. void Delete()
  80. {
  81.     struct Node *temp1;
  82.     int pos,i;
  83.     temp1=head;
  84.     printf("Enter node number for Delete: ");
  85.     scanf("%d",&pos);
  86.     for (i=1; i<pos; i++)
  87.     {
  88.         temp1=temp1->next;
  89.     }
  90.     if (pos==1)
  91.     {
  92.         head= temp1->next;
  93.         temp1->next->prev= temp1->prev;
  94.         free(temp1);
  95.         return ;
  96.     }
  97.     else if (temp1==p)
  98.     {
  99.         temp1=p;
  100.         p=p->prev;
  101.         p->next = NULL;
  102.         free(temp1);
  103.     }
  104.     else
  105.     {
  106.         temp1->prev->next=temp1->next;
  107.         temp1->next->prev=temp1->prev;
  108.         free(temp1);
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement