Ciao a tutti
Allora devo fare un programma che acquisisce due valori (tempo e lunghezza) alla volta.
Questi due valori devono essere ordinati (rispetto al tempo) con gli altri giā acquisiti.
Ogni tot valori (es. 10) deve stampare tutti i valori nella lista.
Il programma termina con i valori 0 e 0 (tempo e lunghezza).
Io ho fatto questo:
codice:
#include <stdio.h>
#include <stdlib.h>
#define NUM 10
typedef struct dati * Dati;
void stampa(Dati head);
struct dati{
double time;
double length;
Dati next;
};
Dati nuovodati (Dati al, double time, double length)
{
al = malloc(sizeof(Dati));
al->time = time;
al->length = length;
al->next = NULL;
return al;
}
Dati inserimento (Dati radice, double time, double length)
{
Dati temp;
Dati prev;
Dati ausiliario;
temp = radice;
if ( radice == NULL )
{
radice = nuovodati(radice, time, length);
}
else if (radice != NULL)
{
while ( temp!=NULL && time>(temp->time) )
{
prev = temp;
temp = temp->next;
}
ausiliario = nuovodati(ausiliario, time, length);
if ( prev == NULL ) radice = ausiliario;
else{
ausiliario = temp;
temp = ausiliario;
}
}
return radice;
};
void stampa(Dati head)
{
Dati temp;
temp = head;
while ( temp != NULL )
{
printf("%f %f", temp->time, temp->length);
temp = temp->next;
}
}
int main()
{
int i;
double time, length;
Dati radice;
radice = NULL;
for (;;)
{
i = 0;
for (; i < NUM; i++)
{
scanf("%f %f", &time, &length);
if ( time != 0 )
{
radice = inserimento(radice, time, length);
}
else if (time == 0) break;
}
stampa(radice);
}
stampa(radice);
system("pause");
exit(0);
}
Ma alla terza coppia il programma termina senza nč stampare nč fare altro...
Non ne capisco il motivo, poteste illuminarmi voi?