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 count the nodes that contain only odd integers from an existing linked list and returns the count.
The method declaration is as follows:
int CountOdd( Node startPtr )
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. Return count
Step 4. Check for odd and increment the counter.
Step 5. Move pointer to the next node
Step 6. End
METHOD:
int CountOdd(Node startPtr)
{
int c=0;
Node temp=new Node(startPtr);
while(temp != null)
{
if (temp.num % 2 != 0)
c++;
temp=temp.next;
}
return c;
}