


Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!



typedef struct elemento { int n ; struct Elemento *next ; } Elemento ; typedef Elemento *Lista ; FUNZIONE CREA NODO (CREA ELEMENTO) Elemento *crea_elemento(int n) { Elemento *elemento ; elemento = malloc( sizeof(Elemento) ) ; if (elemento == NULL) { printf(“Non ho potuto allocare”) ; return NULL ; } elemento —> n = n ; elemento —> next = NULL ; return elemento ; } FUNZIONE INSERIMENTO ELEMENTI IN TESTA void inserimentoInTesta(Lista *l, int n) { Elemento elemento = crea_elemento(n) ; if (l == NULL) { *l = elemento; return ; } elemento —> next = *l ; *l = elemento ; } FUNZIONE INSERIMENTO ELEMENTI IN CODA void inserimentoInCoda(Lista *l, int n) { Elemento elemento = crea_elemento(n) ; if (l == NULL) { *l = elemento; return ; } Elemento *copia_testa = *l ; while (copia_testa —> next != NULL) { copia_testa = copia_testa—> next ; } copia_testa —> next = elemento ;
void inserimentoOrdinato(Lista *l, int n) { Elemento elemento = crea_elemento(n) ; if (l == NULL) { *l = elemento; return ; } Elemento *copia_testa = *l ; if (copia_testa —> n > n) { (qui va messa l’operazione che vogliamo) elemento —> next = *l ; *l = elemento ; return ; } while (copia_testa —> next != NULL) { if ( (copia_testa —> n) <= n && (copia_testa —> next —> n ) >= n) { elemento —> next = copia_testa —> next ; copia_testa —> next = elemento ; return ; } copia_testa = copia_testa —> next ; } copia_testa —> next = elemento ; } FUNZIONE ORDINAMENTO BUBBLE SORT void ordinaLista(Lista l) { if (l == NULL) return ; Elemento *p1 ; Elemento *p2 = NULL ; int swap = 1 ; while (swap) { swap = 0 ; p1 = l ; while (p1 —> next != p2) { if ( (p1 —> n) > (p1 —> next —> n) ) { int temp = p1 —> n ; p1 —> n = p1 —> next —> n ; p1 —> next —> n = temp ; swap = 0 ; } p1 = p1 —> next ; } p2 = p1 ; }
int rimuoviElementoCercato(Lista l , int n) { if (l == NULL) return -1 ; if ( (l —> n) == n) { Elemento elementoS = NULL ; elementoS = (l) —> next ; n = (l) —> n ; free(l) ; *l = elementoS ; return n ; } Elemento *copia_testa = *l ; while (copia_testa —> next != NULL) { if ( (copia_testa —> next —> n ) == n) { Elemento *elementoS = NULL ; elementoS = copia_testa —> next ; n = copia_testa —> next —> n ; copia_testa —> next = copia_testa —> next —> next ; free(elementoS) ; return n ; } copia_testa = copia_testa —> next ; } return -1 ;