-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Arrange consonants and vowels
- Loading branch information
1 parent
561383b
commit abbd2ee
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//Arrange consonants and vowels | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Node { | ||
char data; | ||
Node next; | ||
|
||
public Node(char data){ | ||
this.data = data; | ||
next = null; | ||
} | ||
} | ||
|
||
class GFG { | ||
public static void main (String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
|
||
while(t-- > 0) { | ||
int n = sc.nextInt(); | ||
Node head = null, tail = null; | ||
|
||
char head_c = sc.next().charAt(0); | ||
head = new Node(head_c); | ||
tail = head; | ||
|
||
while(n-- > 1) { | ||
tail.next = new Node(sc.next().charAt(0)); | ||
tail = tail.next; | ||
} | ||
|
||
Solution obj = new Solution(); | ||
show(obj.arrangeCV(head)); | ||
|
||
} | ||
} | ||
|
||
public static void po(Object o) { | ||
System.out.println(o); | ||
} | ||
|
||
public static void show(Node head) { | ||
while(head != null) { | ||
System.out.print(head.data+" "); | ||
head = head.next; | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
Structure of node class is: | ||
class Node { | ||
char data; | ||
Node next; | ||
|
||
public Node(char data) { | ||
this.data = data; | ||
next = null; | ||
} | ||
} | ||
|
||
Solution { | ||
public Node arrangeCV(Node head){ | ||
Node vowel = new Node('a'), con = new Node('b'); | ||
Node vowelH = vowel, conH = con; | ||
|
||
while(head != null) { | ||
char temp = head.data; | ||
|
||
if(temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u') { | ||
vowel.next = head; | ||
vowel = vowel.next; | ||
} | ||
|
||
else { | ||
con.next = head; | ||
con = con.next; | ||
} | ||
|
||
head = head.next; | ||
} | ||
|
||
con.next = null; | ||
vowel.next = conH.next; | ||
|
||
return vowelH.next; | ||
} | ||
} |