Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //List structure:
- struct El {
- int info;
- struct El *next;
- };
- typedef struct El ElementoLista;
- typedef ElementoLista *ListaDiElementi;
- // Functions/Procedure to be implemented:
- void readList(ListaDiElementi *lista)
- {int prec=0;
- int corr=0;
- ListaDiElementi first=malloc(sizeof(ElementoLista));
- ListaDiElementi item=first;
- *lista=first;
- scanf("%d", &corr);
- item->info=corr;
- prec=corr;
- while (5)
- {
- scanf("%d", &corr);
- if (corr>=prec)
- {item->next=malloc(sizeof(ElementoLista)); item=item->next; item->info=corr; item->next= NULL; prec=corr;}
- else if (corr<prec) break;
- }
- };
- void cancellaDuplicates(ListaDiElementi *lista)
- {ListaDiElementi corr;
- ListaDiElementi succ;
- corr=*lista;
- succ=corr->next;
- while (corr->next != NULL)
- {if (succ->info == corr->info)
- {succ=succ->next; free(corr->next); corr->next=succ;}
- else {corr=corr->next; succ=corr->next;}
- }
- };
- void filterLists(ListaDiElementi *lista1, ListaDiElementi lista2)
- {ListaDiElementi corr1, head2, succ1, succ2, head1, prec, elim;
- corr1=*lista1;
- head2=lista2;
- succ1=corr1->next;
- succ2=head2;
- head1=corr1;
- elim=head1;
- //eliminare elementi finche non trovo l'elemento che sarà la testa della lista 1, questo elemento è head1 (p.s. questo pezzo non è definito se non esiste una eventuale testa)
- while (5) {
- if (succ2->info==head1->info ) {elim=head1; succ2=head2; if (head1->next!=NULL) {head1=head1->next; free (elim);} else {free (elim); head1=NULL;break;}}
- else if (succ2->info!=head1->info && succ2->next!=NULL) succ2=succ2->next;
- else if (succ2->info!=head1->info && succ2->next==NULL) {succ2=head2; break;}
- }
- succ2=head2;
- // ora che ho la testa filtrare la lista e cancellare gli elementi a partire dal secondo elemento
- if (head1->next !=NULL) { //verificare che ci sia almeno un altro elemento oltre a head1
- corr1=head1->next;
- prec=head1;
- succ1=corr1->next;
- while (5)
- {if (corr1->next==NULL) //corr1 è l'ultimo elemento
- { if (corr1->info==succ2->info) {free(corr1);prec->next=NULL;}
- else if (succ2->next!=NULL) succ2=succ2->next;
- else {succ2=head2; break;}
- }
- else if (corr1->next!=NULL) //corr1 non è l'ultimo elemento
- { if (corr1->info==succ2->info){prec->next=succ1; free(corr1); corr1=succ1;succ1=corr1->next; succ2=head2;}
- else if (succ2->next==NULL) {prec=prec->next; succ1=succ1->next; corr1=corr1->next;}
- else succ2=succ2->next;
- }
- } //fine while esterno
- } // fine if esterno
- *lista1=head1;
- }; //fine funzione
- //Function to print all the elements of the list:
- void printList(ListaDiElementi list) {
- printf("(");
- while (list != NULL) {
- printf("%d ", list->info);
- list = list->next;
- }
- printf(")\n");
- }
- int main() {
- ListaDiElementi first_list = NULL, second_list=NULL;
- //Read and print the first list:
- readList(&first_list); // add call to procedure/function readList()
- printf("Prima lista\n");
- printList(first_list);
- //Eliminates Duplicates from the first list:
- cancellaDuplicates(&first_list);
- printf("Prima lista senza duplicati\n");
- printList(first_list);
- //Read and print the second list:
- readList(&second_list);
- printf("Seconda lista\n");
- printList(second_list);
- //Eliminates Duplicates from the second list:
- cancellaDuplicates(&second_list);
- printf("Seconda lista senza duplicati\n");
- printList(second_list);
- //Filter the first list using the elements of the second list:
- filterLists(&first_list, second_list);
- //Print the filtered list:
- printf("Lista filtrata\n");
- printList(first_list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement