Ciao...credo che l'errore sia da ricercare nella funzione inserisci testa. Succede che alloca nodi uguali all'ultimo inserito per n(numero inserimenti) volte. Non sono riuscito a correggerlo. ufffff...
codice:
#include <stdio.h>
typedef struct {
char nome[80];
char indirizzo[80];
char telefono[80];
} record;
typedef struct nodo *lista;
struct nodo{
record *elemento;
lista next;
};
typedef enum{OK, ERROR} status;
status allocanodo(lista *r_L, record *d);
status inseriscitesta(lista *r_L, record *d);
void display(lista start);
int main()
{
lista start = NULL;
record r; //usato per le immissioni
char ec;
for(;;){
printf("\n(i)inserisci nuovo nome \n");
printf("(p)stampa a video \n");
printf("(x)uscita dal programma \n");
ec = getche();
switch(ec){
case('i'):
printf("inserisci il nome: ");
gets(r.nome);
printf("inserisci l'indirizzo: ");
gets(r.indirizzo);
printf("inserisci il telefono: ");
gets(r.telefono);
inseriscitesta(&start, &r);
break;
case('p'):
display(start);
break;
case('x'):
exit();
break;
}
getch();
}
getch();
return 0;
}
//inserisce nella lista il nodo al posto giusto
status allocanodo(lista *r_L, record *d){
*r_L = (lista)malloc(sizeof(struct nodo));
if(*r_L==NULL) return ERROR;
else {
(*r_L)->elemento=d;
(*r_L)->next=NULL;
}
return OK;
}
status inseriscitesta(lista *r_L, record *d)
{
lista L;
if (allocanodo(&L,d) == ERROR)
return ERROR;
L-> next = *r_L;
*r_L = L;
return OK;
}
void display(lista start)
{
while(start){
printf("%s\n", (start->elemento)->nome);
printf("%s\n", (start->elemento)->indirizzo);
printf("%s\n\n", (start->elemento)->telefono);
start = start -> next;
}
}
notare che ; ) è stato convertito in sorrisetto!