Idanref

Remove Duplications - Linked List

Apr 7th, 2021 (edited)
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Node
  6. {
  7.     char data;
  8.     struct Node* next;
  9. } node;
  10.  
  11. node* create_node(char data)
  12. {
  13.     node* temp = (node*)malloc(sizeof(node));
  14.  
  15.     temp->data = data;
  16.     temp->next = NULL;
  17.  
  18.     return temp;
  19. }
  20.  
  21. void insert(node** head, char data)
  22. {
  23.     node* temp = *head;
  24.     node* link = create_node(data);
  25.  
  26.     if (*head == NULL)
  27.     {
  28.         *head = link;
  29.         return;
  30.     }
  31.  
  32.     while (temp != NULL)
  33.     {
  34.         if (temp->next == NULL)
  35.         {
  36.             temp->next = link;
  37.             return;
  38.         }
  39.  
  40.         else
  41.             temp = temp->next;
  42.     }
  43. }
  44.  
  45. void print_list(node* head)
  46. {
  47.     if (head == NULL)
  48.     {
  49.         printf("List is empty\n");
  50.         return;
  51.     }
  52.  
  53.     node* temp = head;
  54.  
  55.     while (temp != NULL)
  56.     {
  57.         printf("%c->", temp->data);
  58.         temp = temp->next;
  59.     }
  60.  
  61.     if (temp == NULL)
  62.         printf("NULL");
  63. }
  64.  
  65. void remove_duplications(node** head)
  66. {
  67.     // a>a>a>b>b>b>N
  68.  
  69.     node* i = *head;
  70.     node* j, * temp;
  71.  
  72.     while (i->next != NULL)
  73.     {
  74.         j = i;
  75.  
  76.         while (j->next != NULL)
  77.         {
  78.             if (j->next->data == i->data)
  79.             {
  80.                 temp = j->next;
  81.                 j->next = j->next->next;
  82.                 free(temp);
  83.             }
  84.  
  85.             else if (j->next != NULL)
  86.                 j = j->next;
  87.             else
  88.                 break;
  89.         }
  90.  
  91.         if (i->next)
  92.             i = i->next;
  93.         else
  94.             break;
  95.     }
  96. }
  97.  
  98. void main()
  99. {
  100.     node* head = NULL;
  101.  
  102.     print_list(head);
  103.  
  104.     /*insert(&head, 'a');
  105.     insert(&head, 'b');
  106.     insert(&head, 'd');
  107.     insert(&head, 'b');
  108.     insert(&head, 'c');
  109.     insert(&head, 'c');*/
  110.  
  111.     /*insert(&head, 'a');
  112.     insert(&head, 'a');
  113.     insert(&head, 'b');
  114.     insert(&head, 'b');
  115.     insert(&head, 'c');
  116.     insert(&head, 'a');*/
  117.  
  118.  
  119.     insert(&head, 'a');
  120.     insert(&head, 'b');
  121.     insert(&head, 'b');
  122.     insert(&head, 'a');
  123.     insert(&head, 'b');
  124.     insert(&head, 'b');
  125.  
  126.     /*insert(&head, 'a');
  127.     insert(&head, 'a');
  128.     insert(&head, 'a');
  129.     insert(&head, 'b');
  130.     insert(&head, 'b');
  131.     insert(&head, 'b');*/
  132.  
  133.     remove_duplications(&head);
  134.  
  135.     print_list(head);
  136. }
Add Comment
Please, Sign In to add comment