統計連結串列中母音和子音的數量


在這個問題中,我們需要統計給定連結串列中母音和子音的總數。我們可以遍歷連結串列並檢查每個字元,判斷它是子音還是母音。

問題陳述 - 我們給定一個包含小寫字母字元的連結串列。我們需要統計連結串列中母音和子音的總數。

示例

輸入

'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 - 定義 'listNode' 的 'struct' 型別以建立連結串列節點。

步驟 2 - 定義 addNode() 函式以在連結串列中插入節點。

步驟 2.1 - 在 addNode() 函式中建立一個新節點。同時,用給定的字元初始化其 'ch' 值,並將下一個節點初始化為 null 值。

步驟 2.2 - 如果起始節點為空,則將 temp 節點分配給起始節點。如果起始節點不為空,則遍歷連結串列,找到最後一個節點,並在最後附加 temp 節點。

步驟 3 - 定義 isConst() 變數以檢查特定字元是子音還是母音。

步驟 4 - 定義 countVowelsAndConst() 函式以統計給定字串中母音和子音的總數。

步驟 5 - 將 'cons' 和 'vow' 變數初始化為零,分別儲存子音和母音的計數。

步驟 6 - 如果起始節點為空,則列印一條訊息,說明列表不包含任何母音或子音。

步驟 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),因為計數操作不佔用任何空間。

我們統計了給定連結串列中母音和子音的總數。程式設計師還可以統計給定連結串列中特定字元的頻率,以進行更多練習。

更新於: 2023-08-14

159 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.