Skip to content
Related Articles

Related Articles

Count the number of vowels and consonants in a Linked List

Improve Article
Save Article
Like Article
  • Last Updated : 11 Feb, 2022

Given a linked list containing lowercase English alphabets, the task is to count the number of consonants and vowels present in the linked list.

Example:

Input: Linked List: a ->b->o->y -> e ->z->NULL
Output: 
Vowels : 3
Consonants: 3

Input: Linked List: a -> e -> b->c->s->e->y->t->NULL
Output:
Vowels: 3
Consonants:5

Approach: To solve this problem, follow the below steps:

  1. Create two variables, vowel and consonant to store the number of vowels and consonants respectively. Initialize both of them with 0.
  2. Now, start traversing the linked list, and increment vowel by 1 if the character matches with anyone from the set of [a, e, i, o, u] else increment consonant by 1.
  3. Print the answer according to the above observation.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// A linked list node
struct Node {
    char data;
    struct Node* next;
 
    Node(char key)
    {
        data = key;
        next = NULL;
    }
};
 
// Utility function to check if a character
// is a vowel or not
bool isVowel(char x)
{
    return (x == 'a' || x == 'e'
            || x == 'i' || x == 'o'
            || x == 'u');
}
 
// Function to count the number
// of vowels and consonants
void count(struct Node* head)
{
    int vowel = 0;
    int consonant = 0;
 
    for (Node* itr = head; itr != NULL; itr = itr->next) {
        if (isVowel(itr->data)) {
            vowel++;
        }
        else {
            consonant++;
        }
    }
    cout << "Vowel: " << vowel << endl;
    cout << "Consonant: " << consonant << endl;
}
 
// Driver Code
int main()
{
 
    Node* head = new Node('u');
    head->next = new Node('h');
    head->next->next = new Node('d');
    head->next->next->next = new Node('a');
    head->next->next->next->next = new Node('n');
 
    count(head);
 
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
class GFG
{
 
  // A linked list node
  static class Node {
    char data;
    Node next;
 
    Node(char key)
    {
      data = key;
      next = null;
    }
  };
 
  // Utility function to check if a character
  // is a vowel or not
  static boolean isVowel(char x)
  {
    return (x == 'a' || x == 'e'
            || x == 'i' || x == 'o'
            || x == 'u');
  }
 
  // Function to count the number
  // of vowels and consonants
  static void count(Node head)
  {
    int vowel = 0;
    int consonant = 0;
 
    for (Node itr = head; itr != null; itr = itr.next) {
      if (isVowel(itr.data)) {
        vowel++;
      }
      else {
        consonant++;
      }
    }
    System.out.print("Vowel: " +  vowel +"\n");
    System.out.print("Consonant: " +  consonant +"\n");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    Node head = new Node('u');
    head.next = new Node('h');
    head.next.next = new Node('d');
    head.next.next.next = new Node('a');
    head.next.next.next.next = new Node('n');
 
    count(head);
  }
}
 
// This code is contributed by Rajput-Ji

Python3




# Python program for the above approach
 
# A linked list Node
class Node:
    def __init__(self, data):
        self.data = data;
        self.next = None;
 
# Utility function to check if a character
# is a vowel or not
def isVowel(x):
    return (x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u');
 
# Function to count the number
# of vowels and consonants
def count(head):
    vowel = 0;
    consonant = 0;
 
    while(head!=None):
        if (isVowel(head.data)):
            vowel += 1;
        else:
            consonant += 1;
        head=head.next;
 
    print("Vowel: " , vowel , "");
    print("Consonant: " , consonant , "");
 
# Driver Code
if __name__ == '__main__':
    head = Node('u');
    head.next = Node('h');
    head.next.next = Node('d');
    head.next.next.next = Node('a');
    head.next.next.next.next = Node('n');
 
    count(head);
 
# This code is contributed by 29AjayKumar

C#




// C# program for the above approach
using System;
 
public class GFG
{
 
  // A linked list node
  class Node {
    public char data;
    public Node next;
 
    public Node(char key)
    {
      data = key;
      next = null;
    }
  };
 
  // Utility function to check if a character
  // is a vowel or not
  static bool isVowel(char x)
  {
    return (x == 'a' || x == 'e'
            || x == 'i' || x == 'o'
            || x == 'u');
  }
 
  // Function to count the number
  // of vowels and consonants
  static void count(Node head)
  {
    int vowel = 0;
    int consonant = 0;
 
    for (Node itr = head; itr != null; itr = itr.next) {
      if (isVowel(itr.data)) {
        vowel++;
      }
      else {
        consonant++;
      }
    }
    Console.Write("Vowel: " +  vowel +"\n");
    Console.Write("Consonant: " +  consonant +"\n");
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    Node head = new Node('u');
    head.next = new Node('h');
    head.next.next = new Node('d');
    head.next.next.next = new Node('a');
    head.next.next.next.next = new Node('n');
 
    count(head);
  }
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
       // JavaScript code for the above approach
 
       // A linked list node
       class Node
       {
 
           constructor(key)
           {
               this.data = key;
               this.next = null;
           }
       };
 
       // Utility function to check if a character
       // is a vowel or not
       function isVowel(x)
       {
           return (x == 'a' || x == 'e'
               || x == 'i' || x == 'o'
               || x == 'u');
       }
 
       // Function to count the number
       // of vowels and consonants
       function count(head) {
           let vowel = 0;
           let consonant = 0;
 
           for (let itr = head; itr != null; itr = itr.next) {
               if (isVowel(itr.data)) {
                   vowel++;
               }
               else {
                   consonant++;
               }
           }
           document.write("Vowel: " + vowel + "<br>");
           document.write("Consonant: " + consonant + "<br>");
       }
 
       // Driver Code
       let head = new Node('u');
       head.next = new Node('h');
       head.next.next = new Node('d');
       head.next.next.next = new Node('a');
       head.next.next.next.next = new Node('n');
 
       count(head);
 
 // This code is contributed by Potta Lokesh
   </script>
Output
Vowel: 2
Consonant: 3

Time Complexity: O(N)
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!