Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <float.h>
- #include <malloc.h>
- typedef int boolean;
- #define TRUE 1
- #define FALSE 0
- struct list {
- float value;
- int code;
- struct list * next;
- };
- void init(struct list **ptrptr); //inizializzazione della lista
- void visit_list(struct list *ptr); //funzione di stampa della lista
- void pre_insert( struct list ** ptrptr, float value ); //funzione di inserimento in testa nella lista
- void suf_insert(struct list **ptrptr, float value); //funzione di inserimento in coda nella lista
- void seq_sort_list( struct list ** ptr);
- boolean is_lower(float a, float b);
- void swap(struct list *p1, struct list *p2);
- int main() {
- int M;
- int i;
- float value=0;
- struct list *L;
- init(&L);
- printf("\n Inserire il numero di elementi (maggiori di zero) che si desidera inserire nella lista");
- do {
- scanf("%d", &M);
- if (M <= 0)
- printf("La lista e' vuota!! Inserire un numero di elementi maggiori di zero");
- } while (M <= 0);
- for (i = 0; i < M; i++){
- printf("\nInserire l'elemento numero %d della lista", (i + 1));
- scanf("%f", &value);
- suf_insert(&L, value);
- }
- seq_sort_list(&L);
- visit_list(L);
- return 0;
- }
- void init(struct list **ptrptr){ //inizializzazione della lista
- (*ptrptr)=NULL;
- }
- void visit_list(struct list *ptr){ //funzione di stampa della lista
- while(ptr!=NULL){
- printf(" %f ",ptr->value);
- ptr=ptr->next;
- }
- }
- void pre_insert( struct list **ptrptr, float value ){ //funzione di inserimento in testa nella lista
- struct list * tmp;
- tmp = *ptrptr;
- *ptrptr = (struct list *) malloc( sizeof(struct list));
- (*ptrptr)->value = value;
- (*ptrptr)->next = tmp;
- }
- void suf_insert(struct list **ptrptr, float value){
- while(*ptrptr!=NULL)
- ptrptr=&((*ptrptr)->next);
- pre_insert(ptrptr, value);
- }
- void seq_sort_list(struct list **ptr)
- {
- struct list *min, *ptr1;
- while(*ptr!=NULL)
- {
- min = *ptr;
- ptr1 = (*ptr)->next;
- while(ptr1 != NULL)
- {
- if( is_lower(ptr1->value,min->value))
- {
- min = ptr1;
- }
- ptr1 = ptr1->next;
- }
- swap(*ptr,min);
- ptr = &((*ptr)->next);
- }
- }
- boolean is_lower(float a, float b) {
- if (a<b+ FLT_EPSILON)
- return TRUE;
- else
- return FALSE;
- }
- void swap(struct list *p1, struct list *p2)
- {
- float temp = p1->value;
- p1->value = p2->value;
- p2->value = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement