25/11/08 11:52
gioser
C'erano un po' di errori...
questo dovrebbe funzionare
Questo è un pò più ordinato
questo dovrebbe funzionare
#include <iostream> #include <string> using namespace std; struct lista { string nome; string cognome; int eta; lista *punt; }; int main() { lista *pl; lista **x; int i,k,y; i=0; y=1; cout << "Inserisci il numero di nodi da creare (min 3) = "; cin >>k; lista p; cout <<y<< ") Inserisci il nome = "; cin >> p.nome; cout <<y<< ") Inserisci il cognome = "; cin >> p.cognome; cout <<y<< ") Inserisci l'eta = "; cin >> p.eta; lista q; p.punt =&q; y++; cout <<y<< ") Inserisci il nome = "; cin >> q.nome; cout <<y<< ") Inserisci il cognome = "; cin >> q.cognome; cout <<y<< ") Inserisci l'eta = "; cin >> q.eta; x=&q.punt; //qui c'è l'errore do { lista *r = new lista; r->punt = NULL; *x=r; x = &r->punt; y++; cout <<y<< ") Inserisci il nome = "; cin >> r->nome; cout <<y<< ") Inserisci il cognome = "; cin >> r->cognome; cout <<y<< ") Inserisci l'eta = "; cin >> r->eta; } while(y+1<k); cout<<"STAMPA:"<<endl; for (pl = &p; pl!=NULL; pl = pl->punt) { cout<<pl->nome<<endl; cout<<pl->cognome<<endl; cout<<pl->eta<<endl; } system("pause"); }
Questo è un pò più ordinato
#include <iostream> #include <string> using namespace std; struct lista { string nome; string cognome; int eta; lista *next; }; void leggi_nodo(int y, lista *r) { cout <<y<< ") Inserisci il nome = "; cin >> r->nome; cout <<y<< ") Inserisci il cognome = "; cin >> r->cognome; cout <<y<< ") Inserisci l'eta = "; cin >> r->eta; } void stampa_nodo(lista *pl) { cout<<pl->nome<<endl; cout<<pl->cognome<<endl; cout<<pl->eta<<endl; } int main() { lista *pl = NULL; // elemento corrente della lista lista *head = NULL; // primo elemento della lista lista *tail = NULL; // ultimo elemento della lista int i = 0; int k = 0; int y = 0; cout << "Inserisci il numero di nodi da creare (min 1) = "; cin >>k; for (y = 1; y <= k; ++y) { pl = new lista; pl->next = NULL; if (y == 1) head = pl; else tail->next = pl; leggi_nodo(y, pl); tail = pl; } cout<<"STAMPA:"<<endl; for (pl = head; pl != NULL; pl = pl->next) { stampa_nodo(pl); } for (pl = head; pl != NULL;) { lista *r = pl; pl = pl->next; delete r; } system("pause"); }
Ultima modifica effettuata da gioser 25/11/08 12:47
aaa