Skip to main content

Arrange consonants and vowels nodes in a linked list in swift

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

import Foundation
/*
  Swift 4 program for 
  Arrange consonants and vowels nodes in a linked list
*/
// Linked list node
class LinkNode
{
	var data: Character;
	var next: LinkNode? ;
	init(_ data: Character)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new Node at end of linked list 
	func appendNode(_ node: LinkNode? )
	{
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Handles the request of adding new node in linked list
	func addNode(_ data: Character)
	{
		let node: LinkNode? = LinkNode(data);
		self.appendNode(node);
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.head;
		// iterating linked list elements
		while (temp  != nil)
		{
			print("",temp!.data, terminator: " →");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL\n", terminator: "");
	}
	// Determine that given value is an vowel or not
	func isVowels(_ value: Character) -> Bool
	{
		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
	func arrangeNodes()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		// Define some auxiliary variable
		let vowel: SingleLL? = SingleLL();
		let consonant: SingleLL? = SingleLL();
		var auxiliary: LinkNode? = self.head;
		var ordinary: LinkNode? = nil;
		// iterate linked list nodes
		while (auxiliary  != nil)
		{
			ordinary = auxiliary;
			auxiliary = auxiliary!.next;
			if (self.isVowels(ordinary!.data) == true)
			{
				// Add node into volwel list
				vowel!.appendNode(ordinary);
			}
			else
			{
				consonant!.appendNode(ordinary);
			}
			ordinary!.next = nil;
		}
		if (vowel!.head  != nil)
		{
			// Set new head node of resultant linked list
			self.head = vowel!.head;
			// Connect vowel and consonant linked list
			vowel!.tail!.next = consonant!.head;
			if (consonant!.tail  != nil)
			{
				// When consonant node exists
				// Set new tail node of resultant linked list 
				self.tail = consonant!.tail;
			}
			else
			{
				// When consonant nodes not exists
				self.tail = vowel!.tail;
			}
		}
		else
		{
			// When vowels not exist
			self.head = consonant!.head;
			self.tail = consonant!.tail;
		}
	}
	static func main()
	{
		let 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");
		print(" Before Arrange");
		// R → E → G → U → L → A → T → I → O → N → NULL
		sll.display();
		sll.arrangeNodes();
		print(" After Arrange");
		// E → U → A → I → O → R → G → L → T → N → NULL
		sll.display();
	}
}
SingleLL.main();

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