Dada una lista enlazada que contiene alfabetos ingleses en minúsculas, la tarea es contar el número de consonantes y vocales presentes en la lista enlazada.
Ejemplo:
Entrada: Lista enlazada: a ->b->o->y -> e ->z->NULL
Salida:
Vocales: 3
Consonantes: 3Entrada: Lista enlazada: a -> e -> b->c->s->e->y->t->NULL
Salida:
Vocales: 3
Consonantes: 5
Enfoque: Para resolver este problema, siga los pasos a continuación:
- Cree dos variables, vocal y consonante para almacenar el número de vocales y consonantes respectivamente. Inicialice ambos con 0.
- Ahora, comience a recorrer la lista enlazada e incremente la vocal en 1 si el carácter coincide con cualquiera del conjunto de [a, e, i, o, u]; de lo contrario, incremente la consonante en 1.
- Escriba la respuesta de acuerdo con la observación anterior.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> 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
<script> // JavaScript code for the above approach // A linked list node class Node { constructor(key) { this.data = key; this.next = null; } }; // Utility function to check if a character // is a vowel or not function isVowel(x) { return (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'); } // Function to count the number // of vowels and consonants function count(head) { let vowel = 0; let consonant = 0; for (let itr = head; itr != null; itr = itr.next) { if (isVowel(itr.data)) { vowel++; } else { consonant++; } } document.write("Vowel: " + vowel + "<br>"); document.write("Consonant: " + consonant + "<br>"); } // Driver Code let 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 Potta Lokesh </script>
Producción
Vowel: 2 Consonant: 3
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por akshitsaxenaa09 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA