Advertisement
FalconOS

Untitled

Jan 9th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. struct El {
  13. int info;
  14. struct El *next;
  15. };
  16.  
  17. typedef struct El ElementoLista;
  18.  
  19.  
  20.  
  21. ElementoLista* readList(ElementoLista **lista, int dis){
  22. ElementoLista* head;
  23. ElementoLista* aux= *lista;
  24. ElementoLista* elAdd;
  25. int now;
  26. int last;
  27.  
  28. scanf("%d", &now);
  29. if (dis>=0 && aux==NULL) {
  30. aux = malloc(sizeof(ElementoLista));
  31. head = aux;
  32. }
  33. last = now;
  34.  
  35. while(abs(now-last)<=dis){
  36. last = now;
  37. aux->info = last;
  38. scanf("%d", &now);
  39. if(abs(now-last)<=dis){
  40. elAdd = malloc(sizeof(ElementoLista));
  41. aux->next = elAdd;
  42. aux=aux->next;
  43. }
  44. }
  45.  
  46. aux->next=NULL;
  47.  
  48. return head;
  49.  
  50. };
  51.  
  52.  
  53. int maxDistance(ElementoLista *lista){
  54. int new = lista->info;
  55. int old = lista->info;
  56. int max;
  57. while(lista != NULL){
  58. if(abs(new-old)>max){
  59. max=abs(new-old);
  60. }
  61. old=new;
  62. lista = lista->next;
  63. if(lista!=NULL){
  64. new=lista->info;
  65. }
  66. }
  67.  
  68. return max;
  69. };
  70.  
  71.  
  72. ElementoLista* CancellaMax(ElementoLista **lista, int maxdis){
  73. ElementoLista* head = *lista;
  74. ElementoLista* x = head;
  75. ElementoLista* succ = x->next;
  76. ElementoLista* prec;
  77. ElementoLista* canc;
  78.  
  79. while(succ!=NULL){
  80. if(abs((x->info)-(succ->info))==maxdis){
  81. if(x == head){ // L'elemento da cancellare รจ il primo della lista //
  82. x = x->next;
  83. canc = head;
  84. head = x;
  85. succ = succ->next;
  86. free(canc);
  87. }else{
  88. prec->next=succ;
  89. canc = x;
  90. x = x -> next;
  91. succ = succ->next;
  92. free(canc);
  93. }
  94. }else{
  95. prec=x;
  96. x = x->next;
  97. succ = succ->next;
  98. }
  99. }
  100.  
  101. return head;
  102. };
  103.  
  104.  
  105. void printList(ElementoLista *list){
  106. printf("(");
  107. fflush(stdout);
  108. while (list != NULL){
  109. printf("%d ", list->info);
  110. fflush(stdout);
  111. list = list->next;
  112. }
  113. printf(")\n");
  114. fflush(stdout);
  115. }
  116.  
  117.  
  118.  
  119. int main(){
  120. ElementoLista *list = NULL;
  121. ElementoLista *list_aux = NULL;
  122. int boundis;
  123. int maxdis;
  124.  
  125. scanf("%d", &boundis);
  126.  
  127. list_aux = readList(&list, boundis);
  128.  
  129. printf("La lista bounded-%d e':\n", boundis);
  130. fflush(stdout);
  131.  
  132. if(list_aux != NULL){
  133. list = list_aux;
  134. list_aux = NULL;
  135. }
  136.  
  137. printList(list);
  138.  
  139. maxdis = maxDistance(list);
  140. printf("La distanza massima e':\n%d\n", maxdis);
  141. fflush(stdout);
  142.  
  143. list_aux = CancellaMax(&list, maxdis);
  144. printf("La lista modificata e':\n");
  145. fflush(stdout);
  146. if (list_aux!= NULL){
  147. list = list_aux;
  148. }
  149. printList(list);
  150.  
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement