Advertisement
Nello96

Traccia d'esame 3 - Liste

May 7th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. /* Function:  CoppieCorrispondenti
  2.  * Usage: L = CoppieCorrispondenti (L1, L2)
  3.  * Prototype: lista CoppieCorrispondenti(lista L1, lista L2);
  4.  * ----------------------------------------------------------------------
  5.  * Crea una nuova lista L inserendo in essa quei valori che si trovano sia in L1 che in L2
  6.  * nella stessa posizione (NOTA: sarà preferita una soluzione ricorsiva).
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. typedef struct nodo *lista;
  13.  
  14. typedef struct nodo {
  15.     int val;
  16.     lista next;
  17. } nodo;
  18.  
  19. lista creaLista (void);
  20. lista inserTesta (lista L, int elem);
  21. void inserCoda (lista L, int elem);
  22. void stampaLista (lista L);
  23. lista ultimoNodo (lista L);
  24. lista CoppieCorrispondentiIterativa(lista L1, lista L2);
  25. lista CoppieCorrispondenti(lista L1, lista L2);
  26.  
  27. int main (void) {
  28.     lista L1, L2, L;
  29.     printf("Creazione lista 1:\n");
  30.     L1 = creaLista();
  31.     printf("\nCreazione lista 2:\n");
  32.     L2 = creaLista();
  33.     stampaLista(L1);
  34.     stampaLista(L2);
  35.     printf("\nElenco coppie corrispondenti:\n");   
  36.     L = CoppieCorrispondenti(L1, L2);
  37.     stampaLista(L);
  38. }
  39.  
  40. lista creaLista (void) {
  41.     lista L = NULL;
  42.     nodo N;
  43.     int buffer, primo = 1;
  44.     while (1) {
  45.         printf("Inserisci il prossimo valore (0 per terminare): ");
  46.         scanf("%d", &buffer);
  47.         if (buffer == 0) {
  48.             break;
  49.         }
  50.         else {
  51.             if (primo == 1) {
  52.                 L = inserTesta (L, buffer);
  53.                 primo = 0;
  54.             }
  55.             else {
  56.                 inserCoda (L, buffer);
  57.             }
  58.         }
  59.     }
  60.     return L;
  61. }
  62.  
  63. lista inserTesta (lista L, int elem) {
  64.     nodo *N;
  65.     N = malloc(sizeof(nodo));
  66.     if (N == NULL) {
  67.         printf("Memoria insufficiente\n");
  68.     }
  69.     else {
  70.         N->next = L;
  71.         N->val = elem;
  72.         L = N;
  73.     }
  74.     return L;
  75. }
  76.  
  77. void inserCoda (lista L, int elem) {
  78.     nodo *N;
  79.     nodo *M;
  80.     N = malloc(sizeof(nodo));
  81.     if (N == NULL) {
  82.         return;
  83.     }
  84.     else {
  85.         M = ultimoNodo (L);
  86.         M->next = N;
  87.         N->val = elem;
  88.         N->next = NULL;
  89.     }
  90. }
  91.  
  92. lista ultimoNodo (lista L) {
  93.     while (L->next != NULL) {
  94.         L = L->next;
  95.     }
  96.     return L;
  97. }
  98.  
  99. lista CoppieCorrispondentiIterativa(lista L1, lista L2) {
  100.     lista L = NULL;
  101.     nodo *N;
  102.     int primo = 1;
  103.     while ((L1 != NULL) && (L2 != NULL)) {
  104.         if (L1->val == L2->val) {
  105.             N = malloc(sizeof(nodo));
  106.             if (N == NULL) {
  107.                 printf("Memoria esaurita\n");
  108.                 return L;
  109.             }
  110.             else {
  111.                 if (primo == 1) {
  112.                     N->val = L1->val;
  113.                     N->next = L;
  114.                     L = N;
  115.                     primo = 0;
  116.                 }
  117.                 else {
  118.                     N->val = L1->val;
  119.                     N->next = NULL;
  120.                     L->next = N;
  121.                 }
  122.             }
  123.         }
  124.         L1 = L1->next;
  125.         L2 = L2->next;
  126.     }
  127.     return L;
  128. }
  129.  
  130. lista CoppieCorrispondenti(lista L1, lista L2) {
  131.     lista L = NULL;
  132.     nodo *N;
  133.     if ((L1 == NULL) || (L2 == NULL)) {
  134.         return L;
  135.     }
  136.     else if (L1->val == L2->val) {
  137.         N = malloc(sizeof(nodo));
  138.         if (N == NULL) {
  139.             printf("Memoria esaurita\n");
  140.             return L;
  141.         }
  142.         else {
  143.             N->val = L1->val;
  144.             N->next = L;
  145.             L = N;
  146.         }
  147.     }
  148.     else {
  149.         return CoppieCorrispondenti(L1->next, L2->next);
  150.     }
  151.     L->next = CoppieCorrispondenti(L1->next, L2->next);
  152.     return L;
  153. }
  154.  
  155. void stampaLista (lista L) {
  156.     printf("\nI valori inseriti sono:\n");
  157.     while (L != NULL) {
  158.         printf("%d ", L->val);
  159.         L = L->next;
  160.     }
  161.     printf("\n");
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement