Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 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. scanf("%d", &corr);
  23. item->info=corr;
  24. prec=corr;
  25.  
  26.  
  27. while (5)
  28. {printf("%d\n", item->info);
  29. scanf("%d", &corr);
  30. if (corr>=prec)
  31. {item->next=malloc(sizeof(ElementoLista)); item=item->next; item->info=corr; item->next= NULL; prec=corr;}
  32.  
  33.  
  34. else if (corr<prec) break;
  35.  
  36.  
  37. }
  38.  
  39.  
  40. };
  41.  
  42.  
  43.  
  44.  
  45. void cancellaDuplicates(ListaDiElementi *lista)
  46. {ListaDiElementi corr;
  47. ListaDiElementi succ;
  48. corr=*lista;
  49. succ=corr->next;
  50.  
  51. while (succ->next != NULL)
  52. {if (succ->info == corr->info)
  53. {succ=succ->next; free(corr->next); corr->next=succ;}
  54.  
  55. else {corr=corr->next; succ=corr->next;}
  56.  
  57. }
  58.  
  59.  
  60. };
  61.  
  62.  
  63.  
  64. void filterLists(ListaDiElementi *lista1, ListaDiElementi lista2)
  65. {ListaDiElementi corr1, corr2, succ1, succ2;
  66. corr1=*lista1;
  67. corr2=lista2;
  68.  
  69. while (corr1->next != NULL)
  70. { {if (corr2->info == corr1->info)
  71. {corr1=corr1->next; free(corr1);}
  72.  
  73. else if (corr2->next != NULL)
  74. corr2=corr2->next;
  75. }
  76.  
  77.  
  78. if (corr2->next == NULL)
  79. corr1=corr1->next;
  80.  
  81. if (*lista1==NULL)
  82. lista1=&corr1;
  83. };
  84.  
  85.  
  86. };
  87.  
  88.  
  89. //Function to print all the elements of the list:
  90. void printList(ListaDiElementi list) {
  91. printf("(");
  92. while (list != NULL) {
  93. printf("%d ", list->info);
  94. list = list->next;
  95. }
  96. printf(")\n");
  97. }
  98.  
  99. int main() {
  100. ListaDiElementi first_list = NULL, second_list=NULL;
  101.  
  102. //Read and print the first list:
  103. readList(&first_list); // add call to procedure/function readList()
  104. printf("Prima lista\n");
  105. printList(first_list);
  106.  
  107. //Eliminates Duplicates from the first list:
  108. cancellaDuplicates(&first_list);
  109. printf("Prima lista senza duplicati\n");
  110. printList(first_list);
  111.  
  112.  
  113.  
  114. //Read and print the second list:
  115. readList(&second_list);
  116. printf("Seconda lista\n");
  117. printList(second_list);
  118.  
  119. //Eliminates Duplicates from the second list:
  120. cancellaDuplicates(&second_list);
  121. printf("Seconda lista senza duplicati\n");
  122. printList(second_list);
  123.  
  124.  
  125.  
  126.  
  127. //Filter the first list using the elements of the second list:
  128. filterLists(&first_list, second_list);
  129.  
  130. //Print the filtered list:
  131. printf("Lista filtrata\n");
  132. printList(first_list);
  133.  
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement