Move vowels node at the beginning of linked list in c#
Csharp program for Move vowels node at the beginning of linked list. Here more information.
// Include namespace system
using System;
/*
Csharp program for
Shift the vowels node at beginning of linked list
*/
// Linked list node
public class LinkNode
{
public char data;
public LinkNode next;
public LinkNode(char data)
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public LinkNode head;
public LinkNode tail;
public SingleLL()
{
this.head = null;
this.tail = null;
}
// Add new node at end of linked list
public void addNode(char data)
{
var node = new LinkNode(data);
if (this.head == null)
{
this.head = node;
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
Console.WriteLine("\n Empty linked list");
return;
}
var temp = this.head;
// iterating linked list elements
while (temp != null)
{
Console.Write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
// Determine that given value is an vowel or not
public bool isVowels(char value)
{
if (value == 'a' || value == 'e' ||
value == 'i' || value == 'o' ||
value == 'u' || value == 'A' ||
value == 'E' || value == 'I' ||
value == 'O' || value == 'U')
{
return true;
}
return false;
}
// Move the vowels value at beginning position
public void shiftVowels()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
// Define some auxiliary variable
var node = this.head;
LinkNode back = null;
// iterate linked list nodes
// And shift vowels at front of linked list
while (node != null)
{
if (this.isVowels(node.data) == true && back != null)
{
// Enter here when
// Node is contain vowel and
// its previous (any) node is not form of vowel
if (node == this.tail)
{
// When is last node
// Set new last node
this.tail = back;
}
// Connect previous node to next node
back.next = node.next;
// Connect vowel node to first node
node.next = this.head;
// Set new head
this.head = node;
// Get back node
node = back;
}
else
{
back = node;
}
// Visit to next node
node = node.next;
}
}
public static void Main(String[] args)
{
var sll = new SingleLL();
// Constructed linked list
sll.addNode('E');
sll.addNode('D');
sll.addNode('U');
sll.addNode('C');
sll.addNode('A');
sll.addNode('T');
sll.addNode('I');
sll.addNode('O');
sll.addNode('N');
Console.WriteLine(" Before move vowels");
// E → D → U → C → A → T → I → O → N → NULL
sll.display();
sll.shiftVowels();
Console.WriteLine(" After move vowels");
// O → I → A → U → E → D → C → T → N → NULL
sll.display();
}
}
Output
Before move vowels
E → D → U → C → A → T → I → O → N → NULL
After move vowels
O → I → A → U → E → D → C → T → N → NULL
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment