C++ 在一个链表中计算元音和辅音的数量

C++ 在一个链表中计算元音和辅音的数量

在这个问题中,我们需要计算给定链表中元音和辅音的总数。我们可以遍历链表并检查每个字符是否是辅音或元音。

问题陈述 −我们已经给出了一个包含小写字母字符的链表。我们需要计算链表中元音和辅音的总数。

示例

输入

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

输出

Vowel – 7, constant - 4

解释 − 包含 ‘b’、’c’、’d’和 ‘f’这些常量,其他的是元音字母。

输入

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

结果

Vowel – 5, constant - 0

解释 − 它包含输入字符串中的所有元音字母。

输入

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

输出

Vowel – 0, constant - 7

解释: 输入字符串只包含常量。

方法1

在这个方法中,我们将字符放入数组中。然后,我们将数组转换成链表。接下来,我们遍历链表并检查每个字符是元音还是辅音。

步骤

步骤1: 定义‘struct’类型的‘listNode’以创建链表节点。

步骤2: 定义addNode()函数以在链表中插入节点。

步骤2.1: 在addNode()函数中创建一个新节点。同时,将给定字符的‘ch’值初始化为该新节点,并将下一个节点初始化为null。

步骤2.2: 如果起始节点为null,将临时节点赋给起始节点。如果起始节点不为null,则遍历链表,找到最后一个节点,并在最后附加临时节点。

步骤3: 定义isConst()变量以检查特定字符是辅音还是元音。

步骤4: 定义countVowelsAndConst()函数以计算给定字符串中元音和辅音的总数。

步骤5: 将‘cons’和‘vow’变量初始化为零,以存储辅音和元音的计数。

步骤6: 如果起始节点为null,则打印一条消息,表示列表中不包含任何元音或辅音。

步骤7: 遍历链表,直到达到最后一个节点。获取当前节点的字符,并通过将该字符作为参数传递给isConst()函数来执行检查。

步骤8: 如果isConst()函数返回true,则将‘cons’变量的值增加1。否则,将‘vow’的值增加1。

步骤9: 在循环中移动到下一个节点。

步骤10: 最后,打印‘vow’和‘cons’变量的值。

示例

#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;
}

输出

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

时间复杂度 − O(N) ,因为我们遍历了链表。

空间复杂度 − O(1) ,因为计数操作不需要额外的空间。

我们计算了给定链表中元音字母和辅音字母的总数。程序员还可以练习在给定链表中计算特定字符的频率。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程