Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 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. aux = head;
  36. cout << length(aux);
  37. return 0;
  38. }
  39.  
  40. //lunghezza di una lista
  41. int length(list l) {
  42. if (l) return length(l->next) + 1;
  43. else return 0;
  44. }
  45.  
  46. //stampa una lista
  47. void print(list l) {
  48. if (l) {
  49. cout << l->info << " ";
  50. print(l->next);
  51. }
  52. }
  53.  
  54. //cancella lista
  55. list clear(list & l) {
  56. if (l) {
  57. l->next = clear(l->next);
  58. delete l;
  59. }
  60. return nullptr;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement