Write a sum function in C++ for LinkedBag of integers (must use pointers to traverse the linked list) that will return a sum of all values.
Linked list is a data structure where we have 2 parts one wil store the data value and other will store the address of the next node to establish a link between the nodes
The data in the node can be accesed with arrow operator like node->data it will print the data inside the node .
int
sumfun(
struct
Node* head)
{
struct
Node* temp = head;
int
sum
= 0;
while
(temp != NULL) {
sum
+= temp->data;
temp
= temp->next; // To increment the temp
}
return
sum;
}
This is the required function
Get Answers For Free
Most questions answered within 1 hours.