Docsity

Funzioni con liste in C, Esercizi di Fondamenti di informatica

Toggle sidebar
bg1
1) LISTA CON RELATIVE FUNZIONI
DICHIARAZIONE LISTA
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 ;
}
pf3
pf4

Anteprima parziale del testo

Scarica Funzioni con liste in C e più Esercizi in PDF di Fondamenti di informatica solo su Docsity!

1) LISTA CON RELATIVE FUNZIONI

DICHIARAZIONE LISTA

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 ;

FUNZIONE INSERIMENTO ELEMENTI ORDINATAMENTE

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 ; }

FUNZIONE RIMUOVI ELEMENTO DETERMINATO

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 ;