📌  相关文章
📜  计算链接列表中元音和辅音的数量

📅  最后修改于: 2022-05-13 01:56:10.118000             🧑  作者: Mango

计算链接列表中元音和辅音的数量

给定一个包含小写英文字母的链表,任务是计算链表中出现的辅音和元音的数量。

例子:

方法:要解决此问题,请按照以下步骤操作:

  1. 创建两个变量,元音辅音,分别存储元音和辅音的数量。用 0 初始化它们。
  2. 现在,开始遍历链表,如果字符与 [a, e, i, o, u] 集合中的任何一个字符匹配,则元音加 1,否则辅音加 1。
  3. 根据以上观察打印答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出
Vowel: 2
Consonant: 3

时间复杂度: O(N)
辅助空间: O(1)