Count the Number of Vowels and Consonants in a Linked List


In this problem, we need to count the total vowels and constants in the given linked list. We can traverse the linked list and check for each character, whether it is a constant or vowel.

Problem statement − We have given a linked list containing the lowercase alphabetical characters. We need to count the total number of vowels and constants in the linked list.

Sample examples

Input

'a' -> 'b' -> 'e', 'c' -> 'd' -> 'e' -> 'f'’ -> 'o' -> 'i' -> 'a' -> 'a'

Output

Vowel – 7, constant - 4

Explanation − It contains ‘b’, ‘c’, ‘d’, and ‘f’ constants, and others are vowels.

Input

a’ -> ‘e’ -> ‘i’ -> ‘o’ -> ‘u’

Output

Vowel – 5, constant - 0

Explanation − It contains all vowels in the input string.

Input

'c' -> 'g' -> 'h' -> 'f'’ -> 'p' -> 'q' -> 'w' -> 'x'

Output

Vowel – 0, constant - 7

Explanation − The input string contains only constants.

Approach 1

In this approach, we will take characters in the array. After that, we will convert the array into the linked list. Next, we traverse the linked list and check for each character, whether it is a vowel or a constant.

Algorithm

Step 1 − Define the ‘listNode’ of the ‘struct’ type to create a linked list node.

Step 2 − Define the addNode() function to insert the node in the linked list.

Step 2.1 − Create a new node in the addNode() function. Also, initialize its ‘ch’ value with the given character and the next node with the null value.

Step 2.2 − If the start node is null, assign the temp node to the start node. If the start node is not null, traverse the linked list, find the last node, and append the temp node at last.

Step 3 − Define the isConst() variable to check whether the particular character is constant or vowel.

Step 4 − Define the countVowelsAndConst() function to count the total number of vowels and constants in the given string.

Step 5 − Initialize the ‘cons’ and ‘vow’ variables with zero to store counts of the constant and vowels, respectively.

Step 6 − If the start node is null, print a message that list doesn’t contain any vowels or consonants.

Step 7 − Traverse the linked list until we reach the last node. Get the character of the current node, and execute the isConst() function by passing the character as a parameter.

Step 8 − If isConst() function returns true, increase the value of the ‘cons’ variable by 1. Else, increase the value of the ‘vow’ by 1.

Step 9 − Move to the next node in the loop.

Step 10 − In the last, print the value of the ‘vow’ and ‘cons’ variables.

Example

#include <bits/stdc++.h>
using namespace std;

// creating the struct listNode
struct listNode {
    int ch;
    listNode *next;
} listNode;
// adding nodes to the linked list
void addNode(struct listNode **start, int ch) {
    // creating a new node
    struct listNode *temp = new struct listNode();
    // add ch to the node
    temp->ch = ch;
    temp->next = NULL;

    // if the list is empty, add a node to the list
    if (*start == NULL) {
        *start = temp;
    } else {
        // If the list has some nodes, append the node at the end of the list
        struct listNode *pointer1 = *start;
        while (pointer1->next != NULL) {
            pointer1 = pointer1->next;
        }
        pointer1->next = temp;
    }
}
bool isConst(char ch) {
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
        return false;
    }
    return true;
}
// Counting vowels and consonants
void countVowelsAndConst(struct listNode *start) {
    int vow = 0;
    int cons = 0;
    // if the list is empty
    if (start == NULL) {
        cout << "List is not containing any vowels and constants" << endl;
        return;
    }
    // traverse the linked list
    while (start != NULL) {
        if (isConst(start->ch)) {
            cons++;
        } else {
            vow++;
        }
        start = start->next;
    }
    cout << "Total counts of the vowel in the list is " << vow << endl;
    cout << "Total counts of the consonant in the list is " << cons << endl;
}
int main() {
    int arr[] = {'a', 'b', 'e', 'c', 'd', 'e', 'f', 'o', 'i', 'a', 'a'};
    int len, p;
    // create an empty linked list
    struct listNode *start = NULL;
    len = sizeof(arr) / sizeof(arr[0]);
    // inserting characters of the array to a linked list
    for (p = 0; p < len; p++)
        addNode(&start, arr[p]);
    countVowelsAndConst(start);
    return 0;
}

Output

Total counts of the vowel in the list is 7
Total counts of the consonant in the list is 4

Time complexity− O(N) as we traverse the linked list.

Space complexity − O(1) as the counting operation doesn’t take any space.

We counted the total number of vowels and constants in the given linked list. Programmers can also count the frequency of the particular character in the given linked list for more practice.

Updated on: 14-Aug-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements