Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef int T;
  5. typedef struct cell {
  6. T info;
  7. cell *next;
  8. } * list;
  9.  
  10. int length(list l);
  11. void print(list l);
  12. list clear(list & l);
  13. /*
  14. int main() {
  15. list lista = new cell;
  16. list head = new cell;
  17. list aux = head;
  18. cout << "Numero rounds: ";
  19. int round;
  20. cin >> round;
  21. for (int i = 0; i < round; i++) {
  22. aux->info = i + 1;
  23. aux->next = lista;
  24. lista = aux;
  25. }
  26. aux->next = nullptr;
  27.  
  28. aux = head;
  29. int n = 0;
  30. while ((aux != nullptr) && (n<round)){
  31. print(aux);
  32. n++;
  33. }
  34. cout << endl;
  35.  
  36. cout << length(aux);
  37. return 0;
  38. }*/
  39. int main() {
  40. list aux = new cell; //aux e' una variabile "puntatore", qui stai solo creando una nuova cella di memoria
  41. //letteralmente, new cell non cancella niente ma crea una nuova cella e sposta l'interesse di aux su quella
  42. list lista = aux; //idem per lista, e' un puntatore e con lista = aux fai puntare entrambi sulla stessa cella
  43. list temp = aux; //idem
  44.  
  45. int round;
  46. //int i = 0;
  47. cout << "Numero rounds: ";
  48. cin >> round;
  49.  
  50. /*
  51. for (int i = 0; i < round; i++) {
  52. if (i == 0) {
  53. aux->info = i + 1; //dai una valore alla cella di memoria int(T)
  54. aux = new cell; //crei un nuovo nodo
  55. temp->next = aux; //IMPORTANTE dopo il temp = aux; iniziale, temp e aux (e list) puntano ESATTAMENTE alla stessa cella di memoria
  56. //con temp->next = aux; dici al al valore puntatore della cella a cui punta temp, di prendere l'indirizzo della nuova cella di aux
  57. //temp = temp->next; //qua diciamo a temp di iniziarea puntare alla stessa cella a cui punta aux
  58. temp = aux; //vuol dire la stessa cosa, da testare
  59. }
  60.  
  61. */
  62.  
  63. /*
  64. passaggi identificati per la creazione di una lista
  65. 1.fai puntare aux ad un nuovo nodo, fai puntare temp e head a quel nuovo nodo
  66. 2.dai un valore T(int) alla cella a cui punta aux
  67. 3.crea una nuova cella e falla puntare da aux con aux=new cell;
  68. 4.connetti la vecchia lista con temp->next = aux; , avevi lasciato temp dietro proprio per fare questo
  69. 5.ora sposta temp sulla nuova cella, riportalo a puntare alla stessa cella (nuova) a cui sta puntando aux (dopo il aux = new cell)
  70. con temp = temp->next;
  71. 6.ora puoi continuoare: 1)inserisci valore in aux; 2)crea nuiva cella con aux = new cell;
  72. 3)collega la cella precedente con temp->next = aux; 4)sposta temp sulla nuova cella con temp = temp->next;
  73. */
  74.  
  75. /*
  76. else {
  77. aux->info = i + 1; //associo vauore T alla nuova cella
  78. aux = new cell; // creo un'altra cella
  79. temp->next = aux; //collego le due celle
  80. //temp = temp->next; //porto temp sulla nuova cella
  81. temp = aux;
  82. }
  83. */
  84.  
  85. /*
  86. aux->info = i + 1;
  87. aux = new cell;
  88. i++;
  89. temp->next = aux;
  90. temp = temp->next;
  91. aux->info = i + 1;
  92. aux = new cell;
  93. i++;
  94. temp->next = aux;
  95. temp = temp->next;*/
  96.  
  97. for (int i = 0; i < round; i++) {
  98. aux->info = i + 1;
  99. aux = new cell;
  100. temp->next = aux;
  101. temp = temp->next;
  102. }
  103. aux->next = nullptr;
  104. aux = lista;
  105. print(aux);
  106. //int n = 0;
  107. /*
  108. //while ((aux->next != nullptr) && (n = 0 < round)) {
  109. while (aux->next != nullptr) { //cosi' si ferma dallo stampare quando la lista arriva all'ultimo elemento
  110. cout << aux->info << " ";
  111. aux = aux->next;
  112. n++;
  113. }
  114. */
  115. cout << endl << "Dimensione lista: " << length(aux);
  116.  
  117. }
  118.  
  119. /*
  120. for (int i = 0; i < round; i++) {
  121. if (i == 0) {
  122. aux = new cell;
  123. aux->info = i + 1;
  124. lista = aux;
  125. aux->next = nullptr;
  126. aux = nullptr;
  127. }
  128. else {
  129. aux = new cell;
  130. aux->info = i + 1;
  131. aux->next = lista;
  132. lista = aux;
  133. aux = nullptr;
  134. }
  135.  
  136. }
  137. */
  138. // }
  139.  
  140. //lunghezza di una lista
  141. int length(list l) {
  142. if (l->next != nullptr) return length(l->next) + 1;
  143. else return 0;
  144. }
  145.  
  146. //stampa una lista
  147. void print(list l) {
  148. //if (l) {
  149. // cout << l->info << " ";
  150. // print(l->next);
  151. //}
  152. while (l->next != nullptr) {
  153. cout << l->info << " ";
  154. l = l->next;
  155. }
  156.  
  157. }
  158.  
  159. //cancella lista
  160. list clear(list & l) {
  161. if (l) {
  162. l->next = clear(l->next);
  163. delete l;
  164. }
  165. return nullptr;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement