SHARE
TWEET

Untitled

a guest Feb 12th, 2019 66 in 25 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. // FUN��ES PARA A LISTA
  5. typedef struct NODE
  6. {
  7.     int num;
  8.     struct NODE *next;
  9. } NODE;
  10.  
  11. NODE *creating_element(int n)
  12. {
  13.     NODE *new_node = (NODE *)malloc(sizeof(NODE));
  14.     new_node->num = n;
  15.     new_node->next = NULL;
  16.     return new_node;
  17. }
  18.  
  19. NODE *add_tail(NODE *head, int n)
  20. {
  21.     NODE *aux = head;
  22.     if (aux->next != NULL)
  23.     {
  24.         aux = aux->next;
  25.         return add_tail(head->next, n);
  26.     }
  27.     NODE *new_node = creating_element(n);
  28.     aux->next = new_node;
  29.     return aux;
  30. }
  31.  
  32. void printing_list(NODE *head)
  33. {
  34.     if (head->next != NULL)
  35.     {
  36.         printf("%d\n", head->num);
  37.         printing_list(head->next);
  38.         return;
  39.     }
  40.     printf("%d\n", head->num);
  41. }
  42.  
  43.  
  44. // void bubble_sort(NODE *head, int size, int i)
  45. // {
  46. //     NODE *current = head->next;
  47. //     int aux;
  48. //     if (i <= size)
  49. //     {
  50. //         if (current != NULL)
  51. //         {
  52. //             if (head->num > current->num)
  53. //             {
  54. //                 aux = head->num;
  55. //                 head->num = current->num;
  56. //                 current->num = aux;
  57. //             }
  58. //             bubble_sort(current, size, i);
  59. //         }
  60. //         bubble_sort(head, size, ++i);
  61. //     }
  62. // }
  63.  
  64. void bubble_sort(NODE *head, int size, int i)
  65. {
  66.     NODE *current = head;
  67.     NODE *temp;
  68.     int aux;
  69.     while(current != NULL){
  70.         temp = current->next;
  71.         while(temp != NULL) {
  72.             if(temp->num < current->num){
  73.                 aux = temp->num;
  74.                 temp->num = current->num;
  75.                 current->num = aux;
  76.             }
  77.             temp = temp->next;
  78.         }
  79.         current = current->next;
  80.     }
  81. }
  82.  
  83. int isExisting(NODE *head, int compare)
  84. {
  85.     int i;
  86.     NODE *current = head;
  87.     while (current != NULL)
  88.     {
  89.         if (current->num == compare)
  90.         {
  91.             return 1;
  92.         }
  93.         current = current->next;
  94.     }
  95.     return 0;
  96. }
  97.  
  98. int intersec(NODE *headList1, NODE *headList2, NODE *headListIntersec)
  99. {
  100.     int count = 0;
  101.     NODE *current1 = headList1;
  102.     NODE *current2 = headList2;
  103.  
  104.     while (current1 != NULL)
  105.     {
  106.         while (current2 != NULL)
  107.         {
  108.             if (current1->num == current2->num)
  109.             {
  110.                 if (!isExisting(headListIntersec, current1->num))
  111.                 {
  112.                     if (count == 0)
  113.                     {
  114.                         headListIntersec->num = current1->num;
  115.                     }
  116.                     else
  117.                     {
  118.                         add_tail(headListIntersec, current1->num);
  119.                     }
  120.                     count++;
  121.                 }
  122.             }
  123.             current2 = current2->next;
  124.         }
  125.         current2 = headList2;
  126.         current1 = current1->next;
  127.     }
  128.     return count;
  129. }
  130.  
  131. int main()
  132. {
  133.     int i, n;
  134.     scanf("%d", &n);
  135.     NODE *headList1 = creating_element(n);
  136.     for (i = 0; i < 19; i++)
  137.     {
  138.         scanf("%d", &n);
  139.         add_tail(headList1, n);
  140.     }
  141.  
  142.     scanf("%d", &n);
  143.     NODE *headList2 = creating_element(n);
  144.     for (i = 0; i < 19; i++)
  145.     {
  146.         scanf("%d", &n);
  147.         add_tail(headList2, n);
  148.     }
  149.  
  150.     NODE *headListIntersec = creating_element(-1);
  151.     int count = intersec(headList1, headList2, headListIntersec);
  152.  
  153.     if (count != 0)
  154.     {
  155.         bubble_sort(headListIntersec, count, 0);
  156.         printing_list(headListIntersec);
  157.         printf("\n");
  158.     }
  159.     else
  160.     {
  161.         printf("VAZIO\n");
  162.     }
  163.  
  164.     return 0;
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top