A linked list is formed from the objects of the class Word. The structure of the class Word is…
A linked list is formed from the objects of the class Word. The structure of the class Word is given below:
class Word
{
String value;
Word next;
}Write an Algorithm OR a Method to count and display the number of nodes whose value starts with a consonant.
The method declaration is as follows:
void countConsonant(Word first)Answer
Answer
AIWritten by AI (antigravity) - it can contain mistakes.
void countConsonant(Word first)
{
int count = 0;
Word current = first;
while (current != null)
{
if (current.value != null && current.value.length() > 0)
{
char ch = Character.toUpperCase(current.value.charAt(0));
if (Character.isLetter(ch) && ch != 'A' && ch != 'E' && ch != 'I' && ch != 'O' && ch != 'U')
{
count++;
}
}
current = current.next;
}
System.out.println("Number of nodes starting with a consonant: " + count);
}Explanation: The method traverses the linked list starting from the first node. For each node, it checks if the first character of value is an alphabetic character and not a vowel (A, E, I, O, U). If so, count is incremented. After traversing the list, the count is displayed.From ISC 2026 Computer Science Paper 1, question 11(i).