Can anyone please help me to find the sum in the linked list. I have written codes to insert and remove numbers from lists but I have no idea how to calculate the sum.

void List::insert( const double &value )
{
   ListNode *newPtr = getNewNode( value ); 

   if ( isEmpty() ) 
      firstPtr = lastPtr = newPtr;
   else 
   {
      newPtr->nextPtr = firstPtr; 
      firstPtr = newPtr; 
   }  
}

iterate through the list until the end of the list.
declare a variable called SUM then add the numbers from each node during the iteration to the variable sum.

I came up with this codes here :-

int List::sumOfNodes();
{
	int sum = 0;
	Listnode *currentPtr = firstPtr;
	while( currentPtr ! = null)
	{
		sum = sum + currentPtr;
		currentPtr = currentPtr->nextPtr;
	}
	cout<<"The sum is "<<sum<<endl;
}

comipler is showing me errors!

I came up with this codes here :-

int List::sumOfNodes();
{
	int sum = 0;
	Listnode *currentPtr = firstPtr;
	while( currentPtr ! = null)
	{
		sum = sum + currentPtr;
		currentPtr = currentPtr->nextPtr;
	}
	cout<<"The sum is "<<sum<<endl;
}

comipler is showing me errors!

copy paste the full code.. lets have a look.
and paste the error messages too if possible.

well that was my function and I got this error message

1>c:\users\owner\desktop\new folder\list.h(173) : error C2761: 'int List::sumOfNodes(void)' : member function redeclaration not allowed
1>c:\users\owner\desktop\new folder\list.h(174) : error C2447: '{' : missing function header (old-style formal list?)


I called this method in driver :_

listObject.sumOfNodes();

listObject is an object of type List.

am I doing something wrong in function?

Here are a few errors I can see

int List::sumOfNodes();

remove the ';' at the end.

while( currentPtr ! = null)

null to be written as NULL

sum = sum + currentPtr;

currentPter is of type Listnode not int. You will have to do something like

sum=sum+currentPtr->value;

also the function must return a value.

here is my updated one but still giving me an error :-

int List::sumOfNodes() 
{
	int sum = 0;
	ListNode *currentPtr = firstPtr;
	while( currentPtr != NULL)
	{
		sum = sum + currentPtr;
		currentPtr = currentPtr->nextPtr;
	}
	return sum;
	cout<<"The sum is "<<sum<<endl;
}

here is my updated one but still giving me an error :-

int List::sumOfNodes() 
{
	int sum = 0;
	ListNode *currentPtr = firstPtr;
	while( currentPtr != NULL)
	{
		sum = sum + currentPtr;
		currentPtr = currentPtr->nextPtr;
	}
	return sum;
	cout<<"The sum is "<<sum<<endl;
}

sum = sum + currentPtr;
should be

sum=sum+currentPtr->value; //value=the variable you are using to store the numbers.

sum and currentPtr are of different types.
sum is int and currentPtr->value is an int. but currentPtr is of type ListNode.


after the return statement is encountered the codes after that are inaccessible.
swap the return and cout statements.

I can use same idea to get the average as well?

hmm..
use another variable called int COUNT to count the number of iterations through the list. that will give the number of nodes.

then float avg=sum/COUNT;

:)

Hey! you are not considering all the cases, you are finding the sum of link list, NOT SUM IN THE LINK LIST.

let me give you some example.
lets suppose we have a link list containing following element

1, 5, 4, 3, 2, 8.

and I tell you whether the sum 9 exists or not.
yes! 9 exists, node2 + node3 = 5 + 4 = 9.
but in your case the algorithm fails.

This article has been dead for over six months. Start a new discussion instead.