A linked list is formed from the objects of the class:
class Nodes
{
int num;
Nodes next;
}
Write an Algorithm OR a Method to print the sum of nodes that contains only odd integers of an existing linked list.
The method declaration is as follows:
void NodesCount( Nodes starPtr )
Algorithm to print the sum of the nodes of odd integers in an existing linked list.
Steps :
1 – Start
2 – Set temporary pointer to start node
3 – Repeat steps 4 & 5 until the pointer reaches null. Display the Count. Exit.
4 – Check for odd integers and accumulate
5 – Move pointer to the next node
6 – End
Method to print the sum of the nodes of odd integers in an existing linked list.
void NodeCount( Nodes starPtr)
{
Nodes temp = new Nodes(starPtr)
int c=0;
while (temp!=null)
{
if( temp.num%2 !=0)
c=c+temp.num;
temp=temp.next;
}
System.out.println(c);
}