Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct Anagrafe{
- char *nome;
- char *cognome;
- int anni;
- struct Anagrafe* next;
- } Anagrafe;
- typedef Anagrafe* ListaAnagrafe;
- int uguali(ListaAnagrafe l1, ListaAnagrafe l2){
- if(!strcmp(l1->nome, l2->nome))
- if(!strcmp(l1->cognome, l2->cognome))
- if(l1->anni==l2->anni){
- return 1;
- }
- return 0;
- }
- int append(ListaAnagrafe *l, char *n, char *c, int eta){
- if(*l==NULL){
- (*l)=malloc(sizeof(Anagrafe));
- if((*l)){
- (*l)->next=NULL;
- (*l)->nome=malloc(sizeof(char)*strlen(n)+1);
- if((*l)->nome)
- strcpy((*l)->nome, n);
- else return 0;
- (*l)->cognome=malloc(sizeof(char)*strlen(c)+1);
- if((*l)->cognome)
- strcpy((*l)->cognome, c);
- else return 0;
- (*l)->anni=eta;
- return 1;
- }
- else return 0;
- }
- append(&((*l)->next), n,c,eta);
- }
- int prepend(ListaAnagrafe *l, char *n, char *c, int eta){
- ListaAnagrafe nuova=malloc(sizeof(Anagrafe));
- if(nuova){
- nuova->nome=malloc(sizeof(char)*strlen(n)+1);
- if(nuova->nome){
- nuova->cognome=malloc(sizeof(char)*strlen(c)+1);
- if(nuova->cognome){
- strcpy(nuova->nome, n);
- strcpy(nuova->cognome,c);
- nuova->anni=eta;
- nuova->next=*l;
- *l=nuova;
- return 1;
- }
- else return 0;
- }
- else return 0;
- }
- else return 0;
- }
- void elimina_elemento(ListaAnagrafe *l, ListaAnagrafe elem){
- if((*l)!=NULL){
- elimina_elemento(&((*l)->next), elem);
- if((*l)->next && uguali((*l)->next, elem) && elem){
- ListaAnagrafe tmp=(*l)->next;
- (*l)->next=(*l)->next->next;
- free(tmp->nome);
- free(tmp->cognome);
- free(tmp);
- }
- }
- }
- void togliomonimi(ListaAnagrafe *l){
- ListaAnagrafe scorri=(*l)->next;
- ListaAnagrafe m=*l;
- while(m){
- scorri=m->next;
- while(scorri){
- if(uguali(m,scorri))
- elimina_elemento(l,scorri);
- scorri=scorri->next;
- }
- m=m->next;
- }
- }
- /*
- 2.double media(ListaAnagrafe lista, char iniziale);
- La funzione deve restituire l'età media di tutte le persone descritte nella lista i cui cognomi
- iniziano con il carattere iniziale.
- */
- double media(ListaAnagrafe l, char iniziale){
- double m=0;
- int cont=0;
- while(l){
- if(l->cognome!=NULL && l->cognome[0]==iniziale){
- m = m + l->anni;
- cont++;
- }
- l=l->next;
- }
- return m/cont;
- }
- void inserisci2(ListaAnagrafe *l, ListaAnagrafe elem){
- if(*l && (*l)->next){
- ListaAnagrafe tmp=(*l);
- if(tmp->nome[0]<elem->nome[0]){
- while(tmp && tmp->next->nome[0]<elem->nome[0])
- tmp=tmp->next;
- if(tmp->next->nome[0]<elem->nome[0]){
- ListaAnagrafe amb=tmp;
- prepend(&(tmp), elem->nome, elem->cognome, elem->anni);
- amb->next=tmp;
- }
- }
- else{
- prepend(&(tmp), elem->nome, elem->cognome, elem->anni);
- *l=tmp;
- }
- }
- if(*l && (*l)->next==NULL){
- if((*l)->nome[0] > elem->nome[0]){
- prepend(l,elem->nome, elem->cognome, elem->anni);
- }
- else{
- append(l, elem->nome, elem->cognome, elem->anni);
- }
- }
- if((*l)==NULL){
- printf("Vuota");
- append(l, elem->nome, elem->cognome, elem->anni);
- }
- }
- int inserisci(ListaAnagrafe *a, ListaAnagrafe b){
- if((*a)==NULL || (*a)->next==NULL){
- if((*a)==NULL){
- printf("Caso Lista Vuota\n");
- append(a,b->nome, b->cognome, b->anni);
- return 1;
- }
- if(*a && (*a)->next==NULL){
- printf("Caso Un elemento\n");
- if((*a)->nome[0]>b->nome[0])
- prepend(a,b->nome, b->cognome, b->anni);
- else
- append(a, b->nome, b->cognome, b->anni);
- return 1;
- }
- }
- else{
- printf("Caso Piu elementi\n");
- if(*a && (*a)->nome[0]<b->nome[0]){
- if(*a && (*a)->next->nome[0]<b->nome[0])
- return inserisci(&((*a)->next), b);
- }
- else{
- ListaAnagrafe tmp=*a;
- prepend(&((*a)->next), b->nome, b->cognome, b->anni);
- tmp->next=*a;
- return 1;
- }
- }
- printf("Caso NON DEFINITO");
- return 0;
- }
- ListaAnagrafe unisci(ListaAnagrafe l1, ListaAnagrafe l2){
- ListaAnagrafe m=NULL;
- while(l1){
- inserisci(&m,l1);
- l1=l1->next;
- }
- while(l2){
- inserisci(&m, l2);
- l2=l2->next;
- }
- return m;
- }
- void stampa_lista_anagrafe(ListaAnagrafe l){
- while(l){
- printf("Nome: %s \t Cognome: %s \t Eta: %d\n", l->nome, l->cognome, l->anni);
- l=l->next;
- }
- printf("\nNULL");
- }
- /*==========================================================*/
- //parte alex --------------------------------
- void inserisciTesta(struct Anagrafe **lista,struct Anagrafe b){
- struct Anagrafe *nuovo;
- nuovo=malloc(sizeof(struct Anagrafe));
- nuovo->nome=b.nome;
- nuovo->cognome=b.cognome;
- nuovo->anni=b.anni;
- //inserisco in testa
- if(*lista==NULL){
- *lista=nuovo;
- nuovo->next=NULL;
- return;
- }
- nuovo->next=*lista;
- *lista=nuovo;
- return;
- }
- //inserisci elemento b nella lista a, nella posizione giusta
- void insRec(struct Anagrafe **a,struct Anagrafe b){
- if(*a==NULL)//sono arrivato alla fine o la lista
- //e nulla, quindi termino
- return;
- else{
- int ris;
- if((ris=strcmp((*a)->nome,b.nome))>0){
- inserisciTesta(a,b);
- return;
- }
- else{
- insRec(&((*a)->next),b);
- }
- }
- }
- int main(){
- struct Anagrafe *lista;
- append(&lista,"Aleks","Rossi",23);
- append(&lista,"Bruno","Rossi",23);
- append(&lista,"Mario","Rossi",23);
- printf("\n");
- stampa_lista_anagrafe(lista);
- printf("\n");
- //testo la funzione ricorsiva insRec, che inserisce il nuovo elemento
- //in base al nome, assumendo che sia ordinata la lista
- struct Anagrafe elemento2;
- elemento2.nome="Chiara";
- elemento2.cognome="Rossi";
- elemento2.anni=22;
- insRec(&lista,elemento2);
- printf("\n");
- stampa_lista_anagrafe(lista);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment