Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <float.h>
  4. #include <malloc.h>
  5. typedef int boolean;
  6. #define TRUE 1
  7. #define FALSE 0
  8. struct list {
  9. float value;
  10. int code;
  11. struct list * next;
  12. };
  13.  
  14. void init(struct list **ptrptr); //inizializzazione della lista
  15. void visit_list(struct list *ptr); //funzione di stampa della lista
  16. void pre_insert( struct list ** ptrptr, float value ); //funzione di inserimento in testa nella lista
  17. void suf_insert(struct list **ptrptr, float value); //funzione di inserimento in coda nella lista
  18. void seq_sort_list( struct list ** ptr);
  19. boolean is_lower(float a, float b);
  20. void swap(struct list *p1, struct list *p2);
  21. int main() {
  22. int M;
  23. int i;
  24. float value=0;
  25. struct list *L;
  26. init(&L);
  27. printf("\n Inserire il numero di elementi (maggiori di zero) che si desidera inserire nella lista");
  28. do {
  29. scanf("%d", &M);
  30. if (M <= 0)
  31. printf("La lista e' vuota!! Inserire un numero di elementi maggiori di zero");
  32. } while (M <= 0);
  33. for (i = 0; i < M; i++){
  34. printf("\nInserire l'elemento numero %d della lista", (i + 1));
  35. scanf("%f", &value);
  36. suf_insert(&L, value);
  37. }
  38.  
  39. seq_sort_list(&L);
  40. visit_list(L);
  41. return 0;
  42. }
  43.  
  44. void init(struct list **ptrptr){ //inizializzazione della lista
  45. (*ptrptr)=NULL;
  46. }
  47. void visit_list(struct list *ptr){ //funzione di stampa della lista
  48. while(ptr!=NULL){
  49. printf(" %f ",ptr->value);
  50. ptr=ptr->next;
  51. }
  52. }
  53.  
  54. void pre_insert( struct list **ptrptr, float value ){ //funzione di inserimento in testa nella lista
  55. struct list * tmp;
  56. tmp = *ptrptr;
  57. *ptrptr = (struct list *) malloc( sizeof(struct list));
  58. (*ptrptr)->value = value;
  59. (*ptrptr)->next = tmp;
  60. }
  61. void suf_insert(struct list **ptrptr, float value){
  62. while(*ptrptr!=NULL)
  63. ptrptr=&((*ptrptr)->next);
  64. pre_insert(ptrptr, value);
  65. }
  66.  
  67. void seq_sort_list(struct list **ptr)
  68. {
  69. struct list *min, *ptr1;
  70. while(*ptr!=NULL)
  71. {
  72. min = *ptr;
  73. ptr1 = (*ptr)->next;
  74. while(ptr1 != NULL)
  75. {
  76. if( is_lower(ptr1->value,min->value))
  77. {
  78. min = ptr1;
  79. }
  80. ptr1 = ptr1->next;
  81. }
  82. swap(*ptr,min);
  83. ptr = &((*ptr)->next);
  84.  
  85. }
  86.  
  87. }
  88.  
  89. boolean is_lower(float a, float b) {
  90. if (a<b+ FLT_EPSILON)
  91. return TRUE;
  92. else
  93. return FALSE;
  94. }
  95.  
  96. void swap(struct list *p1, struct list *p2)
  97. {
  98. float temp = p1->value;
  99. p1->value = p2->value;
  100. p2->value = temp;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement