RinoDrummer icon

Esempio Liste

RinoDrummer | PRO | 06/12/17 08:48:42 PM UTC | 0 ⭐ | 324 👁️ | Never ⏰ | []
C |

920 B

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
 
struct Lista {
    int val;
    struct Lista *next;
};
 
struct Lista *creaNodo(int val);
struct Lista *popolaLista(struct Lista *testa, int i);
void stampaLista(struct Lista *testa);
 
int main(void) {
    struct Lista *lista = NULL;
 
    lista = popolaLista(lista, 5);
    stampaLista(lista);
 
    return 0;
}
 
struct Lista *creaNodo(int val) {
    struct Lista *nodo = (struct Lista *) malloc(sizeof(struct Lista));
 
    nodo->val = val;
    nodo->next = NULL;
 
    return nodo;
}
 
struct Lista *popolaLista(struct Lista *testa, int i) {
    testa = NULL;
 
    if (i > 0) {
        testa = creaNodo(i);
        testa->next = popolaLista(testa->next, --i);
    }
 
    return testa;
}
 
void stampaLista(struct Lista *testa) {
    if (testa != NULL) {
        printf("Valore: %d;\n", testa->val);
 
        stampaLista(testa->next);
    }
 
    return;
}

Comments