Advertisement
Motocelium

Contopire benzi

Nov 14th, 2024 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. // main.c
  2.  
  3. #include "functii.h"
  4.  
  5. int main(){
  6.     int n, m;
  7.     NOD *banda1 = NULL, *banda2 = NULL, *banda_contopita = NULL;
  8.     scanf("%d%d", &n, &m);
  9.    
  10.     banda1 = populare_lista(banda1, n);
  11.     banda2 = populare_lista(banda2, m);
  12.    
  13.    
  14.     banda_contopita = contopire_benzi(banda1, banda2, banda_contopita);
  15.    
  16.     afisare(banda_contopita);
  17. }
  18.  
  19. // functii.h
  20.  
  21. #ifndef FUNCTII_H
  22. #define FUNCTII_H
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. typedef struct NOD{
  29.     char nr_inmatriculare[10];
  30.     struct NOD *next;
  31. }NOD;
  32.  
  33. NOD *creare_nod(char *);
  34. NOD *populare_lista(NOD *, int);
  35. NOD *contopire_benzi(NOD *, NOD *, NOD *);
  36. void afisare(NOD *);
  37.  
  38.  
  39. #endif
  40.  
  41. // functii.c
  42.  
  43. #include "functii.h"
  44.  
  45. NOD *creare_nod(char nr[]){
  46.     NOD *new_node = malloc(sizeof(NOD));
  47.     strcpy(new_node -> nr_inmatriculare, nr);
  48.     new_node -> next = NULL;
  49.     return new_node;
  50. }
  51.  
  52. NOD *populare_lista(NOD *head, int n){
  53.    
  54.     char nr[10];
  55.     scanf("%s", nr);
  56.     head = creare_nod(nr);
  57.    
  58.     NOD *nod = head, *new_node = NULL;
  59.    
  60.     for(int i=1; i<n; i++){
  61.         scanf("%s", nr);
  62.         new_node = creare_nod(nr);
  63.         nod->next = new_node;
  64.         nod = nod->next;
  65.     }
  66.    
  67.     return head;
  68. }
  69.  
  70. NOD *contopire_benzi(NOD *b1, NOD *b2, NOD *head){
  71.     NOD *nod1 = b1, *nod2 = b2, *nod, *new_node;
  72.    
  73.     head = creare_nod(nod2->nr_inmatriculare);
  74.     nod2 = nod2 -> next;
  75.     nod = head;
  76.    
  77.     for(int i = 0; nod1 && nod2; i++)
  78.         if(i%2){ // banda 2
  79.             new_node = creare_nod(nod2->nr_inmatriculare);
  80.             nod2 = nod2->next;
  81.             nod -> next = new_node;
  82.             nod = nod -> next;
  83.         }
  84.        
  85.         else{ // banda 1
  86.             new_node = creare_nod(nod1->nr_inmatriculare);
  87.             nod1 = nod1->next;
  88.             nod -> next = new_node;
  89.             nod = nod -> next;
  90.         }
  91.    
  92.     while(nod1){
  93.         new_node = creare_nod(nod1->nr_inmatriculare);
  94.         nod1 = nod1->next;
  95.         nod -> next = new_node;
  96.         nod = nod -> next;    
  97.     }
  98.     while(nod2){
  99.         new_node = creare_nod(nod2->nr_inmatriculare);
  100.         nod2 = nod2->next;
  101.         nod -> next = new_node;
  102.         nod = nod -> next;
  103.     }
  104.    
  105.     return head;
  106. }
  107.  
  108. void afisare(NOD *head){
  109.     NOD *nod = head;
  110.    
  111.     for(; nod; nod = nod -> next)
  112.         printf("%s\n", nod->nr_inmatriculare);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement