Advertisement
Idanref

Keep 1 Char - Linked List

Apr 30th, 2021
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. typedef struct Node
  6. {
  7.     char value;
  8.     struct Node* next;
  9. } node;
  10.  
  11. node* create_node(char data)
  12. {
  13.     node* temp = (node*)malloc(sizeof(node));
  14.  
  15.     temp->value = data;
  16.     temp->next = NULL;
  17.  
  18.     return temp;
  19. }
  20.  
  21. void insert(node** head, char value)
  22. {
  23.     node* link = create_node(value);
  24.  
  25.     if (*head == NULL)
  26.     {
  27.         *head = link;
  28.         return;
  29.     }
  30.  
  31.     node* temp = *head;
  32.  
  33.     while (temp->next != NULL)
  34.     {
  35.         temp = temp->next;
  36.     }
  37.  
  38.     temp->next = link;
  39. }
  40.  
  41. void print_list(node* head)
  42. {
  43.     node* temp = head;
  44.  
  45.     if (head == NULL)
  46.     {
  47.         printf("List is empty.\n");
  48.         return;
  49.     }
  50.  
  51.     while (temp != NULL)
  52.     {
  53.         printf("%c-> ", temp->value);
  54.         temp = temp->next;
  55.     }
  56.     printf("\n");
  57. }
  58.  
  59. void delete_after(node* temp)
  60. {
  61.     if (temp == NULL || temp->next == NULL)
  62.         return;
  63.  
  64.     node* delete_node = temp->next;
  65.     temp->next = temp->next->next;
  66.     free(delete_node);
  67. }
  68.  
  69. void keep_one(node** head, char c)
  70. {
  71.     node* temp = *head;
  72.     int counter = 0;
  73.  
  74.     if (*head == NULL) return;
  75.  
  76.     if ((*head)->value == c)
  77.         counter++;
  78.  
  79.     while (temp->next != NULL)
  80.     {
  81.         if (temp->next->value == c)
  82.         {
  83.             if (counter < 1)
  84.             {
  85.                 counter++;
  86.             }
  87.  
  88.             else
  89.             {
  90.                 delete_after(temp);
  91.                 counter++;
  92.             }
  93.         }
  94.  
  95.         if (temp->next)
  96.         {
  97.             if (temp->next->value == c)
  98.             {
  99.                 if (counter <= 1)
  100.                     temp = temp->next;
  101.             }
  102.  
  103.             else
  104.                 temp = temp->next;
  105.         }
  106.     }
  107. }
  108.  
  109. void removeDupList(node** head)
  110. {
  111.     node* temp = *head;
  112.     while (temp != NULL)
  113.     {
  114.         keep_one(&(*head), temp->value);
  115.         temp = temp->next;
  116.     }
  117. }
  118.  
  119. void removeDupArrayList(node** arr, int size)
  120. {
  121.     for (int i = 0; i < size; i++)
  122.     {
  123.         removeDupList(&(arr[i]));
  124.     }
  125. }
  126.  
  127. void main()
  128. {
  129.     node* head = NULL;
  130.     node* head1 = NULL;
  131.     node* head2 = NULL;
  132.     node* head3 = NULL;
  133.  
  134.     insert(&head, 'a');
  135.     insert(&head, 'c');
  136.     insert(&head, 'a');
  137.     insert(&head, 'b');
  138.     insert(&head, 'b');
  139.     insert(&head, 'a');
  140.  
  141.     insert(&head1, 'c');
  142.     insert(&head1, 'd');
  143.     insert(&head1, 'c');
  144.  
  145.     insert(&head2, 'a');
  146.  
  147.     insert(&head3, 'e');
  148.     insert(&head3, 'e');
  149.     insert(&head3, 'e');
  150.  
  151.     printf("\n\------Old Lists ------\n\n");
  152.  
  153.     print_list(head);
  154.     print_list(head1);
  155.     print_list(head2);
  156.     print_list(head3);
  157.  
  158.     node* arr[4] = { head, head1, head2, head3 };
  159.  
  160.     removeDupArrayList(arr, 4);
  161.  
  162.     printf("\n\n------ New Lists ------\n\n");
  163.  
  164.     print_list(head);
  165.     print_list(head1);
  166.     print_list(head2);
  167.     print_list(head3);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement