Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int T; //definiamo una var T per lo stesso motivo per il quale definivamo
  5. //una const int N quando usavamo gli array statici
  6. typedef struct cell {
  7. T info;
  8. cell * next;
  9. cell * prev;
  10. } * list;
  11.  
  12. //void make(list & aux, list & temp, int num);
  13. //void to_head(list & aux, list & head);
  14. void print(list lista);
  15. int length(list lista);
  16.  
  17. int main() {
  18. list aux = new cell;
  19. list temp = aux;
  20. list head = aux;
  21.  
  22. int index;
  23. int M = 0;
  24. do {
  25. switch (M) {
  26.  
  27. case 0: //Menu', con M inizializzata a 0, a prima run ci entra sempre
  28. cout << endl << "###########################" << endl
  29. << "########## MENU' ##########" << endl
  30. << "1) Crea lista" << endl
  31. << "2) Stampa lista" << endl
  32. << "3) Naviga lista" << endl
  33. << "4) Lunghezza lista" << endl //messa qua temporaneamente, da inglobare in 2)
  34. << "0) Torna al Menu'" << endl
  35. << "Inserisci il numero corrispondente all'azione che vuoi prendere " << endl
  36. << "Inserire un numero non valido fara' terminare il programma" << endl
  37. << ">> ";
  38. cin >> M;
  39. break;
  40.  
  41. case 1:
  42. cout << "Che tipo di lista desideri creare?" << endl
  43. << "1) Crea lista con inserimento manuale voci" << endl
  44. << "2) Crea lista inizializzata a valore 0" << endl
  45. << ">> ";
  46. cin >> M;
  47.  
  48. if (M == 1) {
  49. cout << "Inserisci il numero di nodi iniziali: ";
  50. cin >> index;
  51. for (int i = 0; i < index; i++) {
  52. cout << "Inserisci il valore nella casella numero " << i + 1 << ": ";
  53. if (i < index -1) {
  54. cin >> aux->info;
  55. aux = new cell;
  56. aux->prev = temp;
  57. temp->next = aux;
  58. temp = aux;
  59. }
  60. else {
  61. cin >> aux->info;
  62. aux->next = nullptr;
  63. aux = head;
  64. index = 1;
  65. cout << endl << "~ ~Ho creato la lista e riportato l'interesse in testa~ ~" << endl;
  66. }
  67. }
  68. }
  69. else if (M == 2) {
  70. cout << "Inserisci il numero di nodi iniziali: ";
  71. cin >> index;
  72. for (int i = 0; i < index; i++) {
  73. if (i < index - 1) {
  74. aux->info = 0;
  75. aux = new cell;
  76. aux->prev = temp;
  77. temp->next = aux;
  78. temp = aux;
  79. }
  80. else {
  81. aux->info = 0;
  82. aux->next = nullptr;
  83. aux = head;
  84. index = 1;
  85. cout << endl << "~ ~Ho creato la lista e riportato l'interesse in testa~ ~" << endl;
  86. }
  87. }
  88. }
  89. else {
  90. cout << endl << "Voce Menu' inesistente" << endl << endl;
  91. }
  92. M = 0;
  93. break;
  94.  
  95. case 2:
  96. print(head);
  97. M = 0;
  98. cout << endl;
  99. break;
  100.  
  101. case 3:
  102. cout << "Menu' navigazione lista" << endl
  103. << "1) Naviga uno slot avanti" << endl
  104. << "2) Naviga uno slot dietro" << endl
  105. << "3) Porta l'interesse in testa" << endl
  106. << "4) Porta l'interesse in coda" << endl
  107. << "Attualmente l'interesse e' ";
  108. if (index == 1) cout << "in testa" << endl;
  109. else if (index == length(head)) cout << "in coda" << endl;
  110. else cout << "sulla cella intermedia numero " << index << endl;
  111. cout << "Il valore nella cella interessata e' " << aux->info << endl
  112. << "Che azione vuoi intraprendere? " << endl << ">> ";
  113. cin >> M;
  114. if (M == 1) {
  115. aux = aux->next;
  116. index++;
  117. }
  118. else if (M == 2) {
  119. aux = aux->prev;
  120. index--;
  121. }
  122. else if (M == 3) {
  123. aux = head;
  124. index = 1;
  125. }
  126. else if (M == 4) {
  127. aux = head;
  128. while (aux->next != nullptr) {
  129. aux = aux->next;
  130.  
  131. }
  132. index = length(head);
  133. }
  134. M = 3;
  135. break;
  136.  
  137. case 4:
  138. cout << "La lista ha una lunghezza di " << length(head) << " elementi" << endl;
  139. M = 0;
  140. break;
  141. }
  142.  
  143. /*
  144. cout << "Quanti nodi iniziali deve avere la lista? ";
  145. int index; //L'indice che scorrera' la lista, ti dira' dov'e' attualmente l'interesse
  146. cin >> index;
  147. //crea
  148. //int input;
  149. for (int i = 0; i < index; i++) {
  150. cout << "Inserisci il valore nella casella numero " << i + 1 << ": ";
  151. cin >> aux->info; //input;
  152. //make(aux, temp, input);
  153. //to_head(aux, head);
  154. aux = new cell;
  155. temp->next = aux;
  156. temp = aux;
  157. if (i == index - 1) {
  158. aux->next = nullptr;
  159. aux = head;
  160. index = 1;
  161. cout << endl << "~ ~Ho riportato l'interesse in testa~ ~" << endl;
  162. }
  163. }
  164.  
  165.  
  166. //stampa
  167. print(head);
  168. /*while (aux->next != nullptr) {
  169. cout << aux->info << " ";
  170. aux = aux->next;
  171. }*/
  172.  
  173. } while (M >= 0);
  174.  
  175. cout << endl;
  176. }
  177.  
  178. //crea la lista //ma questo si fa una volta sola, quindi non ha senso...
  179. /*void make(list & aux, list & temp, int num) {
  180. aux = new cell;
  181. aux->info = num;
  182. temp->next = aux;
  183. temp = aux;
  184. }*/
  185.  
  186. //riporta l'interesse in testa //ma e' abbastanza scomodo usarla
  187. //il codice per rifare cio' e' breve ed e' meglio fare il tutto manualmente
  188. /*void to_head(list & aux, list & head) {
  189. aux->next = nullptr;
  190. aux = head;
  191. cout << endl << "~ ~Ho riportato l'interesse in testa~ ~" << endl;
  192. }*/
  193.  
  194. //stampa tutta la lista //la var ausiliaria "lista" puo' essere anche head
  195. //poiche' non e' passata per reference, head non viene davvero spostata
  196. void print(list lista) {
  197. while (lista != nullptr) {
  198. cout << lista->info << " ";
  199. lista = lista->next;
  200. }
  201. }
  202.  
  203. int length(list lista) {
  204. if (lista != nullptr)
  205. return length(lista->next) + 1;
  206. return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement