Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 11(i)

2 marks

Computer ScienceISC 2026 Computer Science Paper 1ProgramData structures

Write the program described below.

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)
Open in the Linked list visualiser

Answer

Answer

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.