Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct list_node_s {
  5.   int value;
  6.   struct list_node_s *next;
  7. } list_node_t;
  8.  
  9.  
  10. list_node_t *subtraction(list_node_t *head1, list_node_t *head2) {
  11.   list_node_t *new_head, *new_ptr, *new_current;
  12.  
  13.   for (list_node_t *ptr1 = head1; ptr1 != NULL; ptr1 = ptr1->next) {
  14.     bool found = false;
  15.  
  16.     for (list_node_t *ptr2 = head1; ptr2 != NULL; ptr2 = ptr2->next) {
  17.       if (ptr1->value == ptr2->value) {
  18.         found = true;
  19.       }
  20.     }
  21.  
  22.     if (found == false) {
  23.       list_node_t *new_ptr = malloc(sizeof(list_node_t));
  24.  
  25.       new_ptr->value = ptr1->value;
  26.       new_ptr->next = NULL;
  27.  
  28.       if (new_head == NULL) {
  29.         new_head = new_ptr;
  30.         new_current = new_ptr;
  31.       } else {
  32.         new_current->next = new_ptr;
  33.         new_current = new_current;
  34.       }
  35.     }
  36.   }
  37.  
  38.   return new_head;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement