Linked List 2019

A linked list is formed from the objects of the class Node. The class structure of the Node is given below:
class Node
{
int num;
Node next;
}
Write an Algorithm OR a Method to find and display the sum of even integers from an existing linked list.
The method declaration is as follows:
void SumEvenNode( Node str )
ALGORITHM:
Step 1. Start
Step 2. Set temporary pointer to the first node
Step 3. Repeat steps 4 and 5 until the pointer reaches null. Display sum, exit
Step 4. check for even number, if found accumulate
Step 5. Move pointer to the next node
Step 6. End algorithm

METHOD: 
void SumEvenNode(Node str) 
{ 
	Node temp=new Node(str); 
	int s=0; 
	while(temp!=null ) 
	{ 
		if (temp.num%2==0) 
			s=s+temp.num; 
		temp=temp.next; 
	} 
	System.out.println(“sum of even integers=”+s); 
} 
This entry was posted in Linked list. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *