Advertisement
fcamuso

Corso recupero C++ - video 27

Dec 6th, 2022
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Calciatore {
  6.   string cognome="";
  7.   int goalSegnati=0;
  8.  
  9.   Calciatore *next = nullptr;
  10. };
  11.  
  12. void ins_testa(Calciatore* &il, Calciatore* nuovo)
  13. {
  14.    nuovo->next = il;
  15.    il = nuovo;
  16. }
  17.  
  18. void ins_coda(Calciatore* &il, Calciatore* nuovo)
  19. {
  20.  
  21.   if (il==nullptr)
  22.   {
  23.     ins_testa(il, nuovo);
  24.   }
  25.   else //spostiamo un puntatore ausiliario sull'ultimo nodo
  26.   {
  27.     Calciatore *temp = il;
  28.  
  29.     while(temp->next != nullptr)
  30.     {
  31.       temp = temp -> next;
  32.     }
  33.  
  34.     temp -> next = nuovo;
  35.   }
  36.  
  37. }
  38.  
  39.  
  40. void stampa_lista(Calciatore* il)
  41. {
  42.   while (il!=nullptr)
  43.   {
  44.     cout << il-> cognome << endl;
  45.     il = il -> next;
  46.   }
  47. }
  48.  
  49. void distruggi_lista(Calciatore* &il)
  50. {
  51.   while (il!=nullptr)
  52.   {
  53.     Calciatore *temp = il;
  54.     il = il -> next;
  55.  
  56.     delete temp;
  57.   }
  58. }
  59.  
  60. void elimina_testa(Calciatore* &il)
  61. {
  62.   if (il!=nullptr)
  63.   {
  64.     Calciatore *temp = il;
  65.     il = il -> next;
  66.    
  67.     delete temp;
  68.        
  69.   }
  70. }
  71.  
  72. void elimina_coda(Calciatore* &il, Calciatore* nuovo)
  73. {
  74.  
  75.   if (il!=nullptr)
  76.   {
  77.     if (il->next == nullptr)
  78.     {
  79.       delete il;
  80.       il=nullptr;      
  81.     }
  82.     else
  83.     {
  84.       Calciatore *temp = il;
  85.  
  86.       while(temp->next->next != nullptr)
  87.       {
  88.         temp = temp -> next;
  89.       }
  90.      
  91.       delete temp->next;
  92.       temp->next = nullptr;    
  93.   }
  94.  
  95. }
  96.  
  97.  
  98. int main()
  99. {
  100.   Calciatore *il = nullptr;
  101.   Calciatore *nuovo = nullptr;
  102.   do {
  103.  
  104.     //creiamo un nodo
  105.     nuovo = new Calciatore;
  106.  
  107.     cout << "Inserire il cognome (stop per uscire): ";
  108.     cin >> nuovo->cognome;
  109.  
  110.     if (nuovo->cognome !="stop")
  111.     {
  112.       cout << "Goal segnati da questo calciatore: ";
  113.       cin >> nuovo->goalSegnati;
  114.  
  115.       ins_coda(il, nuovo);
  116.     }
  117.  
  118.   } while (nuovo->cognome!="stop");
  119.  
  120.   //cout << il->cognome << " - " << il->next->cognome << endl;
  121.   stampa_lista(il);
  122.  
  123.   distruggi_lista(il);
  124.   stampa_lista(il);
  125.  
  126.     return 0;
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement