Advertisement
KeiroKamioka

loop?

Mar 3rd, 2021
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class node
  6. {
  7.     int value;
  8.     node* next;
  9.  
  10.  
  11. public:
  12.     node(int value, node* next)
  13.     {
  14.         this->value = value;
  15.         this->next = next;
  16.     }
  17.  
  18.     void print()
  19.     {
  20.         node* current = this;
  21.  
  22.         while (current != nullptr) {
  23.             cout << current->value << " ";
  24.             current = current->next;
  25.         }
  26.     }
  27.  
  28.     //Add the value end at the end of the list (i.e. after the last element)
  29.     void add(int n)
  30.     {
  31.         // Your code starts here
  32.         node* current = this;
  33.         node* append = new node(n, NULL);
  34.  
  35.         while (current->next != nullptr) {
  36.             current = current->next;
  37.         }
  38.  
  39.         current->next = append;
  40.        
  41.        
  42.         // Your code ends here    
  43.      }
  44.  
  45.        
  46.    
  47.  
  48.     //Return the sum of all the elements in the list
  49.     int sum()
  50.     {
  51.         // Your code starts here
  52.  
  53.         node* current = this;
  54.         int total = 0;
  55.  
  56.         while (current != nullptr) {
  57.             total = total + current->value;
  58.         }
  59.         return total;
  60.  
  61.  
  62.  
  63.         // Your code ends here    
  64.     }
  65.  
  66.     //Multiplies each element in the list by n
  67.     void mult(int n)
  68.     {
  69.         // Your code starts here
  70.  
  71.         node* current = this;
  72.         while (current != nullptr) {
  73.             current->value = current->value * n;
  74.         }
  75.  
  76.  
  77.         // Your code ends here
  78.     }
  79.  
  80.     //Find the maximum value in the list
  81.     int max()
  82.     {
  83.         // Your code starts here
  84.         node* current = this;
  85.         int max = 0;
  86.         while (current != nullptr) {
  87.             if (max < current->value) {
  88.                 max = current->value;
  89.             }
  90.         }
  91.         return max;
  92.  
  93.         // Your code ends here    
  94.     }
  95. };
  96.  
  97. //After
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement