Untitled
unknown
c_cpp
a year ago
1.1 kB
9
Indexable
#include <stdio.h>
#include <stdlib.h>
typedef struct nodo_{
int n;
struct nodo_* next;
} nodo_t;
nodo_t* nuovoNodo(int valore) {
nodo_t* nuovo = (nodo_t*)malloc(sizeof(nodo_t));
if (!nuovo) {
printf("Errore di allocazione memoria.\n");
exit(1);
}
nuovo->n = valore;
nuovo->next = NULL;
return nuovo;
}
nodo_t* inserisciCoda(nodo_t* head, int valore) {
nodo_t* nuovo = nuovoNodo(valore);
if (head == NULL) {
return nuovo;
}
nodo_t* corrente = head;
while (corrente->next != NULL) {
corrente = corrente->next;
}
corrente->next = nuovo;
return head;
}
int main() {
nodo_t* lista = NULL;
lista = inserisciCoda(lista, 4);
lista = inserisciCoda(lista, 5);
lista = inserisciCoda(lista, 3);
lista = inserisciCoda(lista, 7);
lista = inserisciCoda(lista, 2);
nodo_t* risultato = lista_maggiori(lista);
nodo_t* corrente = risultato;
while (corrente != NULL) {
printf("%d ", corrente->n);
corrente = corrente->next;
}
return 0;
}
Editor is loading...
Leave a Comment