Guest User

Untitled

a guest
Jul 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. typedef struct utente //nodo della lista
  7. {
  8. //parte informativa
  9. char nome[20];
  10. char cognome[20];
  11. char telefono[20];
  12. //puntatore al prossimo nodo della lista
  13. struct utente* next;
  14. } UTENTE; //nome del nuovo tipo
  15.  
  16. /*modo alternativo di fare il typedef (stesso significato di sopra)
  17. struct utente
  18. {
  19. char nome[20];
  20. char cognome[20];
  21. char telefono[20];
  22.  
  23. } ;
  24. typedef struct utente UTENTE;
  25. */
  26. //creazione di un nodo
  27. UTENTE* crea_nodo();
  28. //inserimento
  29. UTENTE* ins_in_testa(UTENTE* head, UTENTE * nodo); //aggiunge un nuovo nodo in testa alla lista restituendo la nuova testa
  30. int ins_in_coda(UTENTE* head); //aggiunge un nuovo nodo in coda alla lista restituendo 1 se ok -1 se errore
  31. //stampa
  32. void stampa_nodo(UTENTE *nodo);
  33. void stampa_lista(UTENTE *head);
  34. //cancellazione e altra roba
  35. // TIPO_DI_RITORNO FUNZIONE();
  36. //funzioni di stampa
  37. void stampa_menu();
  38. void stampa_lista(UTENTE *head);
  39.  
  40. int main()
  41. {
  42. char scelta;
  43. UTENTE *head = NULL; //creazione di una lista vuota
  44. head = crea_nodo();
  45. do
  46. {
  47. stampa_menu();
  48. cin>>scelta;
  49. if( (scelta == 'q') || (scelta == 'Q') )break;
  50. switch(scelta)
  51. {
  52. //inserimento in testa
  53. case 't':
  54.  
  55. UTENTE *tmp;
  56. tmp= crea_nodo();
  57. head = ins_in_testa(head, tmp);
  58.  
  59. default: continue;
  60.  
  61.  
  62. }
  63.  
  64. }while(true) ;
  65.  
  66. stampa_lista(head);
  67.  
  68.  
  69.  
  70. system("PAUSE");
  71. }
  72.  
  73.  
  74.  
  75. void stampa_lista(UTENTE *head)
  76. {
  77. if(head == NULL) return;
  78. UTENTE *tmp=head;
  79. while(true)
  80. {
  81. stampa_nodo(tmp);
  82. tmp= tmp->next;
  83. if(tmp == NULL) return;
  84. }
  85. }
  86.  
  87.  
  88. void stampa_nodo(UTENTE *nodo)
  89. {
  90. cout<<"-------STAMPA DI UN UTENTE --------"<<endl;
  91. cout<<"Nome: "<<nodo->nome<<endl;
  92. cout<<"Cognome: "<<nodo->cognome<<endl;
  93. cout<<"Telefono: "<<nodo->telefono<<endl;
  94. cout<<"-------FINE STAMPA DI UN UTENTE --------"<<endl;
  95. }
  96.  
  97. /*primo modo: prendo la head e il puntatore al nodo da inserire;
  98. restituisco il puntatore alla nuova head
  99. */
  100. UTENTE* ins_in_testa(UTENTE* head, UTENTE *nodo) //aggiunge un nuovo nodo in testa alla lista restituendo la nuova testa
  101. {
  102. nodo->next = head;
  103. return nodo;
  104. }
  105.  
  106. UTENTE* crea_nodo()
  107. {
  108. UTENTE* nuovo = (UTENTE*) malloc(sizeof(UTENTE)); //crea un nuovo nodo
  109.  
  110. cout<<"CREAZIONE NUOVO UTENTE"<<endl;
  111. //inizializzazione parte informativa
  112. cout<<"dammi il nome"<<endl;
  113. cin>>(*nuovo).nome; //oppure nuovo->nome
  114. cout<<"dammi il cognome"<<endl;
  115. cin>>nuovo->cognome;
  116. cout<<"dammi il telefono"<<endl;
  117. cin>>nuovo->telefono;
  118. //inizializzo a NULL il puntatore all'elemento successivo
  119. nuovo->next = NULL;
  120. return nuovo;
  121. }
  122.  
  123.  
  124.  
  125. void stampa_menu()
  126. {
  127. cout<<"DIGITARE: "<<endl;
  128. cout<<"t per inserire in testa"<<endl;
  129. cout<<"q per uscire"<<endl;
  130. }
Add Comment
Please, Sign In to add comment