Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //List structure:
  5. struct El {
  6. int info;
  7. struct El *next;
  8. };
  9.  
  10. typedef struct El ElementoLista;
  11. typedef ElementoLista *ListaDiElementi;
  12.  
  13.  
  14. // Functions/Procedure to be implemented:
  15.  
  16. void readList(ListaDiElementi *lista)
  17. {int prec=0;
  18. int corr=0;
  19. ListaDiElementi first=malloc(sizeof(ElementoLista));
  20. ListaDiElementi item=first;
  21. *lista=first;
  22.  
  23. scanf("%d", &corr);
  24. item->info=corr;
  25. prec=corr;
  26.  
  27.  
  28. while (5)
  29. {
  30. scanf("%d", &corr);
  31. if (corr>=prec)
  32. {item->next=malloc(sizeof(ElementoLista)); item=item->next; item->info=corr; item->next= NULL; prec=corr;}
  33.  
  34.  
  35. else if (corr<prec) break;
  36.  
  37. }
  38.  
  39. };
  40.  
  41.  
  42.  
  43.  
  44. void cancellaDuplicates(ListaDiElementi *lista)
  45. {ListaDiElementi corr;
  46. ListaDiElementi succ;
  47. corr=*lista;
  48. succ=corr->next;
  49.  
  50. while (corr->next != NULL)
  51. {if (succ->info == corr->info)
  52. {succ=succ->next; free(corr->next); corr->next=succ;}
  53.  
  54. else {corr=corr->next; succ=corr->next;}
  55.  
  56. }
  57.  
  58.  
  59. };
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. void filterLists(ListaDiElementi *lista1, ListaDiElementi lista2)
  67. {ListaDiElementi corr1, head2, succ1, succ2, head1, prec, elim;
  68. corr1=*lista1;
  69. head2=lista2;
  70. succ1=corr1->next;
  71. succ2=head2;
  72. head1=corr1;
  73. elim=head1;
  74.  
  75.  
  76. //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)
  77.  
  78. while (5) {
  79.  
  80. if (succ2->info==head1->info ) {elim=head1; succ2=head2; if (head1->next!=NULL) {head1=head1->next; free (elim);} else {free (elim); head1=NULL;break;}}
  81. else if (succ2->info!=head1->info && succ2->next!=NULL) succ2=succ2->next;
  82. else if (succ2->info!=head1->info && succ2->next==NULL) {succ2=head2; break;}
  83. }
  84.  
  85.  
  86. succ2=head2;
  87.  
  88.  
  89. // ora che ho la testa filtrare la lista e cancellare gli elementi a partire dal secondo elemento
  90.  
  91.  
  92. if (head1->next !=NULL) { //verificare che ci sia almeno un altro elemento oltre a head1
  93. corr1=head1->next;
  94. prec=head1;
  95. succ1=corr1->next;
  96. while (5)
  97. {if (corr1->next==NULL) //corr1 è l'ultimo elemento
  98. { if (corr1->info==succ2->info) {free(corr1);prec->next=NULL;}
  99. else if (succ2->next!=NULL) succ2=succ2->next;
  100. else {succ2=head2; break;}
  101. }
  102.  
  103.  
  104. else if (corr1->next!=NULL) //corr1 non è l'ultimo elemento
  105. { if (corr1->info==succ2->info){prec->next=succ1; free(corr1); corr1=succ1;succ1=corr1->next; succ2=head2;}
  106. else if (succ2->next==NULL) {prec=prec->next; succ1=succ1->next; corr1=corr1->next;}
  107. else succ2=succ2->next;
  108. }
  109.  
  110.  
  111. } //fine while esterno
  112.  
  113. } // fine if esterno
  114.  
  115.  
  116. *lista1=head1;
  117.  
  118.  
  119. }; //fine funzione
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. //Function to print all the elements of the list:
  134. void printList(ListaDiElementi list) {
  135. printf("(");
  136. while (list != NULL) {
  137. printf("%d ", list->info);
  138. list = list->next;
  139. }
  140. printf(")\n");
  141. }
  142.  
  143.  
  144. int main() {
  145. ListaDiElementi first_list = NULL, second_list=NULL;
  146.  
  147. //Read and print the first list:
  148. readList(&first_list); // add call to procedure/function readList()
  149. printf("Prima lista\n");
  150. printList(first_list);
  151.  
  152. //Eliminates Duplicates from the first list:
  153. cancellaDuplicates(&first_list);
  154. printf("Prima lista senza duplicati\n");
  155. printList(first_list);
  156.  
  157.  
  158.  
  159. //Read and print the second list:
  160. readList(&second_list);
  161. printf("Seconda lista\n");
  162. printList(second_list);
  163.  
  164. //Eliminates Duplicates from the second list:
  165. cancellaDuplicates(&second_list);
  166. printf("Seconda lista senza duplicati\n");
  167. printList(second_list);
  168.  
  169.  
  170.  
  171.  
  172. //Filter the first list using the elements of the second list:
  173. filterLists(&first_list, second_list);
  174.  
  175. //Print the filtered list:
  176. printf("Lista filtrata\n");
  177. printList(first_list);
  178.  
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement