Motocelium

Bingo! - multimi

Nov 22nd, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct NOD{
  5.     int nr;
  6.     struct NOD *next;
  7. }NOD;
  8.  
  9. NOD *creare_nod(int nr){
  10.     NOD *nod_nou = malloc(sizeof(NOD));
  11.     nod_nou -> nr = nr;
  12.     nod_nou -> next = NULL;
  13.     return nod_nou;
  14. }
  15.  
  16. NOD *adauga_numar(NOD *head, int nr){
  17.     NOD *nr_nou = creare_nod(nr), *nod = head;
  18.    
  19.     if(head == NULL)
  20.         return nr_nou;
  21.     if(head -> nr > nr){
  22.         nr_nou -> next = head;
  23.         return nr_nou;
  24.     }
  25.     while(nod->next && nod ->next ->nr < nr) nod = nod-> next;
  26.     nr_nou -> next = nod->next;
  27.     nod -> next = nr_nou;
  28.    
  29.     return head;
  30. }
  31.  
  32. void afisare_lista(NOD *head){
  33.     NOD *nod = head;
  34.    
  35.     for(; nod; nod = nod->next)
  36.         printf("%d ", nod->nr);
  37. }
  38.  
  39. struct NOD *sterge_nod(NOD *head, NOD *sters){
  40.     NOD *nod = head;
  41.     if(head == sters){
  42.         head = head -> next;
  43.         free(sters);
  44.         return head;
  45.     }
  46.    
  47.     for(; nod -> next != sters; nod = nod -> next);
  48.     nod -> next = nod -> next -> next;
  49.     free(sters);
  50.     return head;
  51. }
  52.  
  53. NOD *diferenta(NOD *head1, NOD *head2){
  54.    
  55.     for(NOD *nod2 = head2; nod2; nod2 = nod2 -> next)
  56.         for(NOD *nod1 = head1; nod1; nod1 = nod1 ->next)
  57.             if(nod1 -> nr == nod2 -> nr){
  58.                 head1 = sterge_nod(head1, nod1);
  59.                 break;
  60.             }
  61.     return head1;
  62. }
  63.  
  64. int main(){
  65.     int n, numar;
  66.     NOD *bunica, *numere;
  67.     scanf("%d", &n);
  68.    
  69.     for(int i = 0; i< n*n; i++){
  70.         scanf("%d", &numar);
  71.         bunica = adauga_numar(bunica, numar);
  72.     }
  73.    
  74.    
  75.     while(scanf("%d", &numar) != EOF){
  76.         numere = adauga_numar(numere, numar);
  77.     }
  78.    
  79.     bunica = diferenta(bunica, numere);
  80.    
  81.     if(bunica == NULL)
  82.         printf("BINGO!");
  83.     else
  84.         afisare_lista(bunica);
  85.     return 0;
  86. }
Add Comment
Please, Sign In to add comment