Advertisement
Motocelium

Bill of materials

Nov 22nd, 2024 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. typedef struct NOD{
  7.     char nume[30];
  8.     struct NOD *next;
  9. }NOD;
  10.  
  11. NOD *creare_nod(char *s){
  12.     NOD *nod_nou = malloc(sizeof(NOD));
  13.     strcpy(nod_nou->nume, s);
  14.     nod_nou -> next = NULL;
  15.     return nod_nou;
  16. }
  17.  
  18. NOD *adauga_nod(NOD *head, char *s){
  19.     NOD *nr_nou = creare_nod(s), *nod = head;
  20.    
  21.     if(head == NULL)
  22.         return nr_nou;
  23.        
  24.     while(nod->next) nod = nod-> next;
  25.     nod -> next = nr_nou;
  26.    
  27.     return head;
  28. }
  29.  
  30. void afisare_lista(NOD *head){
  31.     NOD *nod = head;
  32.    
  33.     for(; nod; nod = nod->next)
  34.         printf("%s\n", nod->nume);
  35. }
  36.  
  37. int intersect(NOD *head1, NOD *head2){
  38.     int nr = 0;
  39.     for(NOD *nod1 = head1; nod1; nod1 = nod1 -> next)
  40.         for(NOD *nod2 = head2; nod2; nod2 = nod2 -> next)
  41.             if(strcmp(nod1->nume, nod2->nume) == 0){
  42.                 nr++;
  43.                 break;
  44.             }
  45.     return nr;
  46. }
  47.  
  48. int main() {
  49.     int n;
  50.     char s[30];
  51.     NOD *lista_cumparaturi = NULL, *lista_furnizor = NULL;
  52.  
  53.     scanf("%d", &n);
  54.     for (int i = 0; i < n; i++) {
  55.         scanf("%s", s);
  56.         lista_cumparaturi = adauga_nod(lista_cumparaturi, s);
  57.     }
  58.  
  59.     while (scanf("%s", s) != EOF) {
  60.         lista_furnizor = adauga_nod(lista_furnizor, s);
  61.     }
  62.  
  63.     int nr_produse = intersect(lista_cumparaturi, lista_furnizor);
  64.     printf("%d\n", nr_produse);
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement