Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class node
- {
- int value;
- node* next;
- public:
- node(int value, node* next)
- {
- this->value = value;
- this->next = next;
- }
- void print()
- {
- node* current = this;
- while (current != nullptr) {
- cout << current->value << " ";
- current = current->next;
- }
- }
- //Add the value end at the end of the list (i.e. after the last element)
- void add(int n)
- {
- // Your code starts here
- node* current = this;
- node* append = new node(n, NULL);
- while (current->next != nullptr) {
- current = current->next;
- }
- current->next = append;
- // Your code ends here
- }
- //Return the sum of all the elements in the list
- int sum()
- {
- // Your code starts here
- node* current = this;
- int total = 0;
- while (current != nullptr) {
- total = total + current->value;
- }
- return total;
- // Your code ends here
- }
- //Multiplies each element in the list by n
- void mult(int n)
- {
- // Your code starts here
- node* current = this;
- while (current != nullptr) {
- current->value = current->value * n;
- }
- // Your code ends here
- }
- //Find the maximum value in the list
- int max()
- {
- // Your code starts here
- node* current = this;
- int max = 0;
- while (current != nullptr) {
- if (max < current->value) {
- max = current->value;
- }
- }
- return max;
- // Your code ends here
- }
- };
- //After
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement