Skip to main content

Arrange consonants and vowels nodes in a linked list in php

Php program for Arrange consonants and vowels nodes in a linked list. Here problem description and explanation.

<?php
/*
  Php program for 
  Arrange consonants and vowels nodes in a linked list
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class SingleLL
{
	public $head;
	public $tail;
	public	function __construct()
	{
		$this->head = NULL;
		$this->tail = NULL;
	}
	// Add new Node at end of linked list 
	public	function appendNode($node)
	{
		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
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		$this->appendNode($node);
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == NULL)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->head;
		// iterating linked list elements
		while ($temp != NULL)
		{
			echo " $temp->data →";
			// Visit to next node
			$temp = $temp->next;
		}
		echo " NULL\n";
	}
	// Determine that given value is an vowel or not
	public	function isVowels($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;
	}
	// Arrange the nodes in front position of vowels 
	// and end with consonants
	public	function arrangeNodes()
	{
		if ($this->head == NULL)
		{
			echo "\n Empty linked list\n";
			return;
		}
		// Define some auxiliary variable
		$vowel = new SingleLL();
		$consonant = new SingleLL();
		$auxiliary = $this->head;
		$ordinary = NULL;
		// 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;
		}
	}
	public static
	function main($args)
	{
		$sll = new 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');
		echo " Before Arrange\n";
		// R → E → G → U → L → A → T → I → O → N → NULL
		$sll->display();
		$sll->arrangeNodes();
		echo " After Arrange\n";
		// E → U → A → I → O → R → G → L → T → N → NULL
		$sll->display();
	}
}
SingleLL::main(array());

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