PDA

View Full Version : programma...


Mac85
23-06-2006, 16:01
ciao a tutti,sono nuovo del forum e spero di trovarmi bene...ho un problemino che mi attanaglia da ore....
Perchè il programma(scritto in c) qui sotto non funziona correttamente?la compilazione la esegue correttamente,ed anche l'esecuzione ,tranne quando si cerca di inserire un nodo nella lista...nella funzione insert....se magari qualcuno riuscisse a spiegarmi questo gliene sarei davvero grato.....




#include <iostream.h>
#include <stdlib.h>

typedef struct rec *lista;

typedef struct rec{
char nome[20];
lista next;
}classe;

lista cima=NULL;

void insert();
void elimina();
void stampa(lista entry);
void menu();

void insert(){
lista prec,cur,nuovo;
int i;
FILE *f1;
f1=fopen("ciao.txt","r");
i=fgetc(f1);
cout<<"i vale="<<i;
for(int a=0;a<i;a++){
nuovo=(lista)malloc(sizeof(classe));
if(cima==NULL){
cout<<"lista vuota"<<endl;
cima=nuovo;
menu();
}else{
prec=NULL;
cur=cima;
}
fgets(nuovo->nome,20,f1);
fgetc(f1);
while((cur!=NULL)&&(strcmp(cur->nome,nuovo->nome)<0)){
prec=cur;
cur=cur->next;
}
if(cur==NULL)
nuovo=prec->next;
else{
nuovo=prec->next;
nuovo->next=cur;
}
}
fclose(f1);
free(nuovo);
}



void elimina(){
lista prec,cur;
char nom[20];
cout<<endl<<"inserire nome da eliminare:"<<endl;
cin>>nom;
if(cima==NULL){
cout<<"lista vuota...impossibile eliminare.."<<endl;
menu();}
else{
prec=NULL;
cur=cima;
while((cur!=NULL)&&(strcmp(nom,cur->nome)!=0)){
prec=cur;
cur=cur->next;
}
if(cur==NULL){
cout<<"non esiste il nome digitato nella lista"<<endl;
menu();}
else{
cout<<endl<<"nome trovato!!!!"<<endl;
prec->next=cur->next;
}
}
menu();
}

void stampa(lista entry){
if(cima==NULL){
cout<<"lista vuota"<<endl;
menu();
}
else{
cout<<entry->nome<<endl;
if(entry->next==NULL){
cout<<endl<<"fine stampa"<<endl;
menu();}
else
stampa(entry->next);
}}



void menu(){
int a;
cout<<"LISTA classe"<<endl<<endl<<endl;
cout<<"1)insert nodo"<<endl<<"2)elimina nodo"<<endl<<"3)stampa"<<endl<<"4)esci"<<endl<<endl;
do{
cin>>a;
}while((a!=1)&&(a!=2)&&(a!=3)&&(a!=4));
switch(a){
case 1:insert();
menu();
break;
case 2:elimina();
menu();
break;
case 3:stampa(cima);
menu();
break;
case 4:break;
}}





int main (int argc, char * const argv[]) {
menu();
return 0;
}

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.