Skip to main content

Arrange consonants and vowels nodes in a linked list in kotlin

Kotlin program for Arrange consonants and vowels nodes in a linked list. Here more information.

/*
  Kotlin program for 
  Arrange consonants and vowels nodes in a linked list
*/
// Linked list node
class LinkNode
{
	var data: Char;
	var next: LinkNode ? ;
	constructor(data: Char)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	fun appendNode(node: LinkNode ? ): Unit
	{
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail?.next = node;
		}
		this.tail = node;
	}
	// Handles the request of adding new node in linked list
	fun addNode(data: Char): Unit
	{
		val node: LinkNode = LinkNode(data);
		this.appendNode(node);
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			println("\n Empty linked list");
			return;
		}
		var temp: LinkNode ? = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Determine that given value is an vowel or not
	fun isVowels(value: Char): Boolean
	{
		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;
	}
	// Arrange the nodes in front position of vowels 
	// and end with consonants
	fun arrangeNodes(): Unit
	{
		if (this.head == null)
		{
			println("\n Empty linked list");
			return;
		}
		// Define some auxiliary variable
		val vowel: SingleLL = SingleLL();
		val consonant: SingleLL = SingleLL();
		var auxiliary: LinkNode ? = this.head;
		var ordinary: LinkNode ? ;
		// iterate linked list nodes
		while (auxiliary != null)
		{
			ordinary = auxiliary;
			auxiliary = auxiliary.next;
			if (this.isVowels(ordinary.data) == true)
			{
				// Add node into volwel list
				vowel.appendNode(ordinary);
			}
			else
			{
				consonant.appendNode(ordinary);
			}
			ordinary.next = null;
		}
		if (vowel.head != null)
		{
			// Set new head node of resultant linked list
			this.head = vowel.head;
			// Connect vowel and consonant linked list
			vowel.tail?.next = consonant.head;
			if (consonant.tail != null)
			{
				// When consonant node exists
				// Set new tail node of resultant linked list 
				this.tail = consonant.tail;
			}
			else
			{
				// When consonant nodes not exists
				this.tail = vowel.tail;
			}
		}
		else
		{
			// When vowels not exist
			this.head = consonant.head;
			this.tail = consonant.tail;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val sll: SingleLL = SingleLL();
	// Constructed linked list
	sll.addNode('R');
	sll.addNode('E');
	sll.addNode('G');
	sll.addNode('U');
	sll.addNode('L');
	sll.addNode('A');
	sll.addNode('T');
	sll.addNode('I');
	sll.addNode('O');
	sll.addNode('N');
	println(" Before Arrange");
	// R → E → G → U → L → A → T → I → O → N → NULL
	sll.display();
	sll.arrangeNodes();
	println(" After Arrange");
	// E → U → A → I → O → R → G → L → T → N → NULL
	sll.display();
}

Output

 Before Arrange
 R → E → G → U → L → A → T → I → O → N → NULL
 After Arrange
 E → U → A → I → O → R → G → L → T → N → NULL




Comment

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