kebria

Deleting the middle/intermediate node of the list using doubly linked list

Jun 16th, 2021 (edited)
1,099
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Deleting the middle/intermediate node of the list using doubly linked list */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct node {
  6.     struct node *prev;
  7.     int data;
  8.     struct node *next;
  9. };
  10.  
  11. struct node *addToEmpty(struct node *head, int data)
  12. {
  13.     struct node *temp = malloc(sizeof(struct node));
  14.  
  15.     temp->prev = NULL;
  16.     temp->data = data;
  17.     temp->next = NULL;
  18.  
  19.     head = temp;
  20.     return head;
  21. }
  22.  
  23. struct node* addAtEnd(struct node* head, int data)
  24. {
  25.     struct node *temp, *tp;
  26.     temp = malloc(sizeof(struct node));
  27.  
  28.     temp->prev = NULL;
  29.     temp->data = data;
  30.     temp->next = NULL;
  31.  
  32.     tp = head;
  33.     while (tp->next != NULL)
  34.     {
  35.         tp = tp->next;
  36.     }
  37.     tp->next = temp;
  38.     temp->prev = tp;
  39.     return head;
  40. }
  41.  
  42. struct node* delFirst(struct node* head)
  43. {
  44.     struct node* temp = head;
  45.     head = head->next;
  46.     free(temp);
  47.     return head;
  48. }
  49.  
  50. struct node* delLast(struct node* head)
  51. {
  52.     struct node* temp = head;
  53.     struct node* temp2;
  54.     while (temp->next != NULL)
  55.         temp = temp->next;
  56.     temp2 = temp->prev;
  57.     temp2->next = NULL;
  58.     free(temp);
  59.     return head;
  60. }
  61.  
  62. struct node* delInter(struct node* head, int position)
  63. {
  64.     struct node* temp = head;
  65.     struct node* temp2 = NULL;
  66.     if (position == 1)
  67.     {
  68.         head = delFirst(head);
  69.         return head;
  70.     }
  71.    
  72.     while (position > 1)
  73.     {
  74.         temp = temp->next;
  75.         position--;
  76.     }
  77.  
  78.     if (temp->next == NULL)
  79.         head = delLast(head);
  80.     else
  81.     {
  82.         temp2 = temp->prev;
  83.         temp2->next = temp->next;
  84.         temp->next->prev = temp2;
  85.         free(temp);
  86.         temp = NULL;
  87.     }
  88.     return head;
  89. }
  90.  
  91. void print(struct node* head)
  92. {
  93.     struct node* ptr = head;
  94.     while (ptr != NULL)
  95.     {
  96.         printf("%d ", ptr->data);
  97.         ptr = ptr->next;
  98.     }
  99.     printf("\n");
  100. }
  101.  
  102. int main()
  103. {
  104.     struct node *head = NULL;
  105.     struct node *ptr;
  106.     head = addToEmpty(head, 24);
  107.     head = addAtEnd(head, 45);
  108.     head = addAtEnd(head, 9);
  109.    
  110.     printf("Before Deletion: ");
  111.     print(head);
  112.  
  113.     head = delInter(head, 2);
  114.     printf("After Deletion: ");
  115.     print(head);
  116.  
  117.     return 0;
  118. }
  119.  
  120.  
  121. /*
  122. Output:
  123. Before Deletion : 24 45 9
  124. After Deletion : 24 9
  125. */
RAW Paste Data