
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Arrange consonants and vowels nodes in a linked list in C++?
In this technique, we transfer the nodes having vowels as keys to the beginning and consonants to the end. In this case we also maintain the order. Example is given below −
Input: A-M-A-Z-O-N Output: A-A-O-M-Z-N Code (Complexity: O(N), Space O(1))
Example
#include<iostream> using namespace std; class Node1{ public: char var1; Node1 *next1; Node1(char v,Node1 *next1=NULL):var1(v),next1(next1){} }; Node1 *make_list(char array1[],int size1){ if(size1 ==0) return NULL; else { Node1 *head = new Node1('o'); Node1 *temp = head; for(int i=0;i<size;++i) { temp->next1 = new Node1(array1[i]); temp=temp->next1; } temp=head; head = head->next1; delete temp; return head; } } void print_list(Node1 *head){ while(head){ cout<<head->var1<<"--"; head = head->next1; } cout<<"END"<<endl; } void insertAfter(Node1** temp,Node1 *n){ n->next1 = (*temp)->next1; (*temp)->next1 = n; } bool isVowel(char v){ switch(v){ case 'A': case 'E': case 'I': case 'O': case 'U': return true; default: return false; } } Node1 *groupByVowels(Node1 *head){ Node1 *vowel=NULL,*consonant=NULL; vowel = new Node1('L'); consonant = new Node1('C'); Node1 *tv = vowel,*tc=consonant; for(Node1 *temp=head;temp;){ Node1 *tt = temp->next1; if(isVowel(temp->var1)){ insertAfter(&tv,temp); tv = tv->next1; } Else { insertAfter(∓tc,temp); tc=tc->next1; } temp = tt; } tv->next1 = consonant->next1; tv = vowel; vowel=vowel->next1; delete tv; return vowel; } int main(){ char array1[] = {'A','M','A','Z','O','N'}; Node1 *head = make_list(array1,sizeof(array1)/sizeof(array1[0])); print_list(head); head = groupByVowels(head); print_list(head); }
Output
A--M--A--Z--O--N--END A--A--O--M--Z--N--END
- Related Articles
- Alternating Vowels and Consonants in C/C++
- Program to arrange linked list nodes based on the value k in Python
- Count nodes in Circular linked list in C++
- Delete N nodes after M nodes of a linked list in C++?
- Delete alternate nodes of a Linked List in C++
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Frequency of vowels and consonants in JavaScript
- Validating alternating vowels and consonants in JavaScript
- Delete N nodes after M nodes of a linked list in C++ program
- Find sum of even and odd nodes in a linked list in C++
- Reverse Alternate K Nodes in a Singly Linked List in C++
- C# Program to count number of Vowels and Consonants in a string
- Print alternate nodes of a linked list using recursion in C++
- Sum of the nodes of a Circular Linked List in C++
- Delete all Prime Nodes from a Doubly Linked List in C++

Advertisements