Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. //listanomi.cpp: lista ordinata di nomi
  2.  
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. // struttura per i nodi della lista
  8. struct Nodo {
  9. string nome;
  10. Nodo*succ;
  11. };
  12.  
  13. Nodo*testa; // puntatore alla testa della lista
  14.  
  15. // prototipi delle funzioni
  16. void Inizializza();
  17. bool ListaVuota();
  18. void Inserisci();
  19. void Elimina();
  20. void Visualizza();
  21. void SvuotaLista();
  22. void PresentaMenu();
  23.  
  24. // funzione principale
  25. int main()
  26. {
  27. int scelta; //scelta nel menu
  28.  
  29. Inizializza();
  30. do{
  31. do{
  32. PresentaMenu();
  33. cout<< " Inserisci la tua scelta ";
  34. cin>> scelta;
  35. }while (scelta<1 || scelta>5);
  36. switch(scelta) {
  37. case 1:
  38. Inserisci();
  39. break;
  40. case 2 :
  41. Elimina();
  42. break;
  43. case 3:
  44. Visualizza();
  45. break;
  46. case 4:
  47. SvuotaLista();
  48. break;
  49. }
  50. }while (scelta != 5);
  51. SvuotaLista();
  52.  
  53. return 0;
  54. }
  55.  
  56. // Inizializzazione della lista
  57. void Inizializza()
  58. {
  59. testa = NULL;
  60. }// Inizializza
  61.  
  62. //controllo se lista รจ vuota
  63. bool ListaVuota()
  64. {
  65. //restituisce false se non vuota
  66. // true se vuota
  67. if (testa == NULL) return true;
  68.  
  69. return false;
  70. } // ListaVuota
  71.  
  72. // inserimento di un nodo nella lista
  73. void Inserisci()
  74. {
  75. string NomeInp; // nome da inserire
  76. Nodo*nuovo; // puntatore ad un nuovo nodo
  77. Nodo*temp; // puntatori per confronti
  78. Nodo*prec;
  79.  
  80. cout<< " Nome da inserire: ";
  81. cin>> NomeInp;
  82. nuovo= new Nodo; // alloca la memoria per il nuovo nodo
  83. nuovo -> nome = NomeInp;
  84. nuovo -> succ = NULL;
  85. if (ListaVuota()) testa = nuovo;
  86. else{
  87. if (testa -> nome >= nuovo -> nome) {
  88. temp = testa;
  89. testa = nuovo;
  90. testa -> succ = temp;
  91. }
  92. else{
  93. temp= testa;
  94. while (temp != NULL && nuovo -> nome >= temp -> nome) {
  95. prec = temp;
  96. temp = temp -> succ;
  97. }
  98. nuovo -> succ = temp;
  99. prec -> succ = nuovo;
  100. }
  101. }
  102. } // Inserisci
  103.  
  104. // estrazione di un nodo della lista
  105. void Elimina()
  106. {
  107. string NomeInp; // nome da eliminare
  108. Nodo*temp; // puntatori per confronti
  109. Nodo* prec;
  110. bool trovato = false; //indicatore se trovato
  111.  
  112. cout << " Nome da eliminare: ";
  113. cin >> NomeInp;
  114. if(ListaVuota()) cout << " Lista vuota " << endl;
  115. else{
  116. if (testa -> nome == NomeInp) {
  117. trovato = true;
  118. temp = testa -> succ;
  119. cout << " Nome eliminato: " << testa -> nome << endl;
  120. delete testa;
  121. testa = temp;
  122. }
  123. else{
  124. temp = testa -> succ;
  125. prec = testa;
  126. do{
  127. if (temp -> nome == NomeInp) {
  128. trovato = true;
  129. prec -> succ = temp -> succ;
  130. cout << "nome eliminato: " << temp -> nome << endl;
  131. delete temp;
  132. }
  133. else{
  134. prec = temp;
  135. temp = temp -> succ;
  136. }
  137. }while (!trovato && temp != NULL);
  138. }
  139. if (!trovato)
  140. cout << " Il nome richiesto non esiste " << endl;
  141. } // fine else
  142. } // Elimina
  143.  
  144. // Visualizzazione della lista
  145. void Visualizza()
  146. {
  147. Nodo*p;
  148. cout << " -- Elenco dei nomi: -- " << endl;
  149. p = testa;
  150. while ( p != NULL ) {
  151. cout << p -> nome << endl;
  152. p = p -> succ;
  153. }
  154. cout << " -- Fine elenco: --" << endl;
  155. } // Visualizza
  156.  
  157. // toglie tutti i nodi alla lista
  158. void SvuotaLista()
  159. {
  160.  
  161. Nodo* temp;
  162. while ( testa != NULL) {
  163. cout << " Nome eliminato: " << testa -> nome << endl;
  164. temp = testa;
  165. testa = testa -> succ;
  166. delete temp;
  167. };
  168. cout << " Lista vuota " << endl;
  169. } // SvuotaLista
  170.  
  171. // menu delle scelte
  172. void PresentaMenu()
  173. {
  174. cout << endl << "-----------" << endl;
  175. cout << " 1. Inserisci un nome " << endl;
  176. cout << " 2. Elimina un nome " << endl;
  177. cout << " 3. Visualizza la lista " << endl;
  178. cout << " 4. Svuota la lista " << endl;
  179. cout << " 5. Fine " << endl;
  180. cout << " ------------------" << endl;
  181. } // PresentaMenu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement