AleksandarArkan

L - Inserisci nome in lista ordinata

May 17th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.77 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. typedef struct Anagrafe{
  6.     char *nome;
  7.     char *cognome;
  8.     int anni;
  9.     struct Anagrafe* next;
  10. } Anagrafe;
  11. typedef Anagrafe* ListaAnagrafe;
  12.  
  13. int uguali(ListaAnagrafe l1, ListaAnagrafe l2){
  14.     if(!strcmp(l1->nome, l2->nome))
  15.         if(!strcmp(l1->cognome, l2->cognome))
  16.             if(l1->anni==l2->anni){
  17.                 return 1;
  18.             }
  19.  
  20.     return 0;
  21. }
  22. int append(ListaAnagrafe *l, char *n, char *c, int eta){
  23.     if(*l==NULL){
  24.         (*l)=malloc(sizeof(Anagrafe));
  25.         if((*l)){
  26.             (*l)->next=NULL;
  27.             (*l)->nome=malloc(sizeof(char)*strlen(n)+1);
  28.             if((*l)->nome)
  29.                 strcpy((*l)->nome, n);
  30.             else return 0;
  31.  
  32.             (*l)->cognome=malloc(sizeof(char)*strlen(c)+1);
  33.             if((*l)->cognome)
  34.                 strcpy((*l)->cognome, c);
  35.             else return 0;
  36.             (*l)->anni=eta;
  37.  
  38.             return 1;
  39.         }
  40.         else return 0;
  41.     }
  42.     append(&((*l)->next), n,c,eta);
  43. }
  44.  
  45. int prepend(ListaAnagrafe *l, char *n, char *c, int eta){
  46.     ListaAnagrafe nuova=malloc(sizeof(Anagrafe));
  47.     if(nuova){
  48.         nuova->nome=malloc(sizeof(char)*strlen(n)+1);
  49.         if(nuova->nome){
  50.             nuova->cognome=malloc(sizeof(char)*strlen(c)+1);
  51.             if(nuova->cognome){
  52.                 strcpy(nuova->nome, n);
  53.                 strcpy(nuova->cognome,c);
  54.                 nuova->anni=eta;
  55.                 nuova->next=*l;
  56.                 *l=nuova;
  57.                 return 1;
  58.             }
  59.             else return 0;
  60.         }
  61.         else return 0;
  62.     }
  63.     else return 0;
  64. }
  65.  
  66. void elimina_elemento(ListaAnagrafe *l, ListaAnagrafe elem){
  67.     if((*l)!=NULL){
  68.         elimina_elemento(&((*l)->next), elem);
  69.         if((*l)->next && uguali((*l)->next, elem) && elem){
  70.             ListaAnagrafe tmp=(*l)->next;
  71.             (*l)->next=(*l)->next->next;
  72.             free(tmp->nome);
  73.             free(tmp->cognome);
  74.             free(tmp);
  75.         }
  76.     }
  77. }
  78.  
  79. void togliomonimi(ListaAnagrafe *l){
  80.     ListaAnagrafe scorri=(*l)->next;
  81.     ListaAnagrafe m=*l;
  82.     while(m){
  83.         scorri=m->next;
  84.         while(scorri){
  85.             if(uguali(m,scorri))
  86.                 elimina_elemento(l,scorri);
  87.             scorri=scorri->next;
  88.         }
  89.     m=m->next;
  90.     }
  91. }
  92.  
  93.  
  94. /*
  95. 2.double media(ListaAnagrafe lista, char iniziale);
  96. La funzione deve restituire l'età media di tutte le persone descritte nella lista i cui cognomi
  97. iniziano con il carattere iniziale.
  98. */
  99.  
  100. double media(ListaAnagrafe l, char iniziale){
  101.     double m=0;
  102.     int cont=0;
  103.     while(l){
  104.         if(l->cognome!=NULL && l->cognome[0]==iniziale){
  105.             m = m + l->anni;
  106.             cont++;
  107.         }
  108.         l=l->next;
  109.     }
  110.     return m/cont;
  111.  
  112. }
  113.  
  114. void inserisci2(ListaAnagrafe *l, ListaAnagrafe elem){
  115.     if(*l && (*l)->next){
  116.         ListaAnagrafe tmp=(*l);
  117.         if(tmp->nome[0]<elem->nome[0]){
  118.             while(tmp && tmp->next->nome[0]<elem->nome[0])
  119.                 tmp=tmp->next;
  120.             if(tmp->next->nome[0]<elem->nome[0]){
  121.                 ListaAnagrafe amb=tmp;
  122.                 prepend(&(tmp), elem->nome, elem->cognome, elem->anni);
  123.                 amb->next=tmp;
  124.             }
  125.         }
  126.         else{
  127.             prepend(&(tmp), elem->nome, elem->cognome, elem->anni);
  128.             *l=tmp;
  129.         }
  130.     }
  131.     if(*l && (*l)->next==NULL){
  132.         if((*l)->nome[0] > elem->nome[0]){
  133.             prepend(l,elem->nome, elem->cognome, elem->anni);
  134.         }
  135.         else{
  136.             append(l, elem->nome, elem->cognome, elem->anni);
  137.         }
  138.     }
  139.     if((*l)==NULL){
  140.         printf("Vuota");
  141.        append(l, elem->nome, elem->cognome, elem->anni);
  142.     }
  143. }
  144.  
  145. int inserisci(ListaAnagrafe *a, ListaAnagrafe b){
  146.     if((*a)==NULL || (*a)->next==NULL){
  147.         if((*a)==NULL){
  148.             printf("Caso Lista Vuota\n");
  149.             append(a,b->nome, b->cognome, b->anni);
  150.             return 1;
  151.  
  152.         }
  153.         if(*a && (*a)->next==NULL){
  154.                 printf("Caso Un elemento\n");
  155.             if((*a)->nome[0]>b->nome[0])
  156.                 prepend(a,b->nome, b->cognome, b->anni);
  157.             else
  158.                 append(a, b->nome, b->cognome, b->anni);
  159.  
  160.         return 1;
  161.         }
  162.     }
  163.     else{
  164.             printf("Caso Piu elementi\n");
  165.         if(*a && (*a)->nome[0]<b->nome[0]){
  166.             if(*a && (*a)->next->nome[0]<b->nome[0])
  167.                 return inserisci(&((*a)->next), b);
  168.         }
  169.         else{
  170.             ListaAnagrafe tmp=*a;
  171.             prepend(&((*a)->next), b->nome, b->cognome, b->anni);
  172.             tmp->next=*a;
  173.             return 1;
  174.         }
  175.     }
  176.     printf("Caso NON DEFINITO");
  177.     return 0;
  178. }
  179.  
  180. ListaAnagrafe unisci(ListaAnagrafe l1, ListaAnagrafe l2){
  181.     ListaAnagrafe m=NULL;
  182.     while(l1){
  183.         inserisci(&m,l1);
  184.         l1=l1->next;
  185.     }
  186.  
  187.  
  188.  
  189.     while(l2){
  190.         inserisci(&m, l2);
  191.         l2=l2->next;
  192.     }
  193.     return m;
  194. }
  195.  
  196. void stampa_lista_anagrafe(ListaAnagrafe l){
  197.     while(l){
  198.         printf("Nome: %s \t Cognome: %s \t Eta: %d\n", l->nome, l->cognome, l->anni);
  199.         l=l->next;
  200.     }
  201.     printf("\nNULL");
  202. }
  203. /*==========================================================*/
  204. //parte alex --------------------------------
  205.  
  206. void inserisciTesta(struct Anagrafe **lista,struct Anagrafe b){
  207.     struct Anagrafe *nuovo;
  208.     nuovo=malloc(sizeof(struct Anagrafe));
  209.     nuovo->nome=b.nome;
  210.     nuovo->cognome=b.cognome;
  211.     nuovo->anni=b.anni;
  212.    
  213.     //inserisco in testa
  214.     if(*lista==NULL){
  215.         *lista=nuovo;
  216.         nuovo->next=NULL;
  217.         return;
  218.     }
  219.     nuovo->next=*lista;
  220.     *lista=nuovo;
  221.     return;
  222. }
  223.  
  224.  
  225. //inserisci elemento b nella lista a, nella posizione giusta
  226. void insRec(struct Anagrafe **a,struct Anagrafe b){
  227.     if(*a==NULL)//sono arrivato alla fine o la lista
  228.     //e nulla, quindi termino
  229.         return;
  230.     else{
  231.         int ris;
  232.         if((ris=strcmp((*a)->nome,b.nome))>0){
  233.             inserisciTesta(a,b);
  234.             return;
  235.             }
  236.         else{
  237.             insRec(&((*a)->next),b);
  238.             }
  239.        
  240.         }
  241.     }
  242. int main(){
  243.     struct Anagrafe *lista;
  244.     append(&lista,"Aleks","Rossi",23);
  245.     append(&lista,"Bruno","Rossi",23);
  246.     append(&lista,"Mario","Rossi",23);
  247.     printf("\n");
  248.     stampa_lista_anagrafe(lista);
  249.     printf("\n");
  250.    
  251.     //testo la funzione ricorsiva insRec, che inserisce il nuovo elemento
  252.     //in base al nome, assumendo che sia ordinata la lista
  253.    
  254.     struct Anagrafe elemento2;
  255.     elemento2.nome="Chiara";
  256.     elemento2.cognome="Rossi";
  257.     elemento2.anni=22;
  258.    
  259.     insRec(&lista,elemento2);
  260.     printf("\n");
  261.    
  262.    
  263.        
  264.     stampa_lista_anagrafe(lista);
  265. return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment