计算链接列表中元音和辅音的数量
给定一个包含小写英文字母的链表,任务是计算链表中出现的辅音和元音的数量。
例子:
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
方法:要解决此问题,请按照以下步骤操作:
- 创建两个变量,元音和辅音,分别存储元音和辅音的数量。用 0 初始化它们。
- 现在,开始遍历链表,如果字符与 [a, e, i, o, u] 集合中的任何一个字符匹配,则元音加 1,否则辅音加 1。
- 根据以上观察打印答案。
下面是上述方法的实现:
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)