Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <iostream>
- using namespace std;
- typedef struct utente //nodo della lista
- {
- //parte informativa
- char nome[20];
- char cognome[20];
- char telefono[20];
- //puntatore al prossimo nodo della lista
- struct utente* next;
- } UTENTE; //nome del nuovo tipo
- /*modo alternativo di fare il typedef (stesso significato di sopra)
- struct utente
- {
- char nome[20];
- char cognome[20];
- char telefono[20];
- } ;
- typedef struct utente UTENTE;
- */
- //creazione di un nodo
- UTENTE* crea_nodo();
- //inserimento
- UTENTE* ins_in_testa(UTENTE* head, UTENTE * nodo); //aggiunge un nuovo nodo in testa alla lista restituendo la nuova testa
- int ins_in_coda(UTENTE* head); //aggiunge un nuovo nodo in coda alla lista restituendo 1 se ok -1 se errore
- //stampa
- void stampa_nodo(UTENTE *nodo);
- void stampa_lista(UTENTE *head);
- //cancellazione e altra roba
- // TIPO_DI_RITORNO FUNZIONE();
- //funzioni di stampa
- void stampa_menu();
- void stampa_lista(UTENTE *head);
- int main()
- {
- char scelta;
- UTENTE *head = NULL; //creazione di una lista vuota
- head = crea_nodo();
- do
- {
- stampa_menu();
- cin>>scelta;
- if( (scelta == 'q') || (scelta == 'Q') )break;
- switch(scelta)
- {
- //inserimento in testa
- case 't':
- UTENTE *tmp;
- tmp= crea_nodo();
- head = ins_in_testa(head, tmp);
- default: continue;
- }
- }while(true) ;
- stampa_lista(head);
- system("PAUSE");
- }
- void stampa_lista(UTENTE *head)
- {
- if(head == NULL) return;
- UTENTE *tmp=head;
- while(true)
- {
- stampa_nodo(tmp);
- tmp= tmp->next;
- if(tmp == NULL) return;
- }
- }
- void stampa_nodo(UTENTE *nodo)
- {
- cout<<"-------STAMPA DI UN UTENTE --------"<<endl;
- cout<<"Nome: "<<nodo->nome<<endl;
- cout<<"Cognome: "<<nodo->cognome<<endl;
- cout<<"Telefono: "<<nodo->telefono<<endl;
- cout<<"-------FINE STAMPA DI UN UTENTE --------"<<endl;
- }
- /*primo modo: prendo la head e il puntatore al nodo da inserire;
- restituisco il puntatore alla nuova head
- */
- UTENTE* ins_in_testa(UTENTE* head, UTENTE *nodo) //aggiunge un nuovo nodo in testa alla lista restituendo la nuova testa
- {
- nodo->next = head;
- return nodo;
- }
- UTENTE* crea_nodo()
- {
- UTENTE* nuovo = (UTENTE*) malloc(sizeof(UTENTE)); //crea un nuovo nodo
- cout<<"CREAZIONE NUOVO UTENTE"<<endl;
- //inizializzazione parte informativa
- cout<<"dammi il nome"<<endl;
- cin>>(*nuovo).nome; //oppure nuovo->nome
- cout<<"dammi il cognome"<<endl;
- cin>>nuovo->cognome;
- cout<<"dammi il telefono"<<endl;
- cin>>nuovo->telefono;
- //inizializzo a NULL il puntatore all'elemento successivo
- nuovo->next = NULL;
- return nuovo;
- }
- void stampa_menu()
- {
- cout<<"DIGITARE: "<<endl;
- cout<<"t per inserire in testa"<<endl;
- cout<<"q per uscire"<<endl;
- }
Add Comment
Please, Sign In to add comment