31/05/09 10:08
Chiara87
Devo semplicemente scrivere una funzione che mi inverte i VALORI all'interno di una lista dinamica
Questo è il codice completo
Il problema però risiede solamente nella funzione invertiLista infatti non mi viene stampato nulla, qualcuno saprebbe dirmi cosa sbaglio?
Grazie
Questo è il codice completo
#include <stdio.h> #include <stdlib.h> typedef struct nd { int val; struct nd *next; } TABR; TABR* insertOrdina (TABR *l, int v) { TABR *k, *t = (TABR *) malloc (sizeof(TABR)); if (t != NULL) { t->val = v; t->next = NULL; if (l == NULL) return t; else { if (v <= l->val) { t->next = l; return t; } else { k=l; while (l->next != NULL && v > l->next->val) { l=l->next; } t->next = l->next; l->next = t; return k; } } } else return l; } TABR *invertiLista(TABR *l) { int val; while (l != NULL) { TABR *t =l; val = t->val; t->val = t->next->val; t->next->val = val; t=t->next; } return l; } void stampaListaR(TABR *l) { if (l != NULL) { printf("%d\n", l->val); stampaListaR(l->next); } } int main () { TABR *lint = NULL; FILE *fr; FILE *fw; int i, v; fw = fopen ("dati.txt", "w"); printf("Scrivi 10 numeri nel file:\n\n"); if (fw != NULL) { for (i=1; i<=10; ++i) { scanf("%d", &v); fprintf(fw, "%d\n", v); } } fclose(fw); fr = fopen ("dati.txt", "r"); if (fr != NULL) { while ((fscanf(fr, "%d", &v)) != EOF) { lint = insertOrdina(lint, v); } } fclose(fr); printf("\n\nI VALORI DEL FILE ORDINATI (IN MODO CRESCENTE) NELLA LISTA SONO:\n\n"); stampaListaR(lint); lint = invertiLista(lint); printf("\n\nI VALORI INVERTITI DELLA LISTA SONO:\n\n"); stampaListaR(lint); system ("pause"); return 0; }
Il problema però risiede solamente nella funzione invertiLista infatti non mi viene stampato nulla, qualcuno saprebbe dirmi cosa sbaglio?
Grazie
aaa