Skip to main content

Move vowels node at the beginning of linked list in php

Php program for Move vowels node at the beginning of linked list. Here problem description and other solutions.

<?php
/*
  Php program for
  Shift the vowels node at beginning of 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 addNode($data)
	{
		$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	function display()
	{
		if ($this->head == NULL)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->head;
		// iterating linked list elements
		while ($temp != NULL)
		{
			echo " ".strval($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;
	}
	// Move the vowels value at beginning position
	public	function shiftVowels()
	{
		if ($this->head == NULL)
		{
			echo "\n Empty linked list\n";
			return;
		}
		// Define some auxiliary variable
		$node = $this->head;
		$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
	function main($args)
	{
		$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');
		echo " Before move vowels", "\n";
		// E → D → U → C → A → T → I → O → N → NULL
		$sll->display();
		$sll->shiftVowels();
		echo " After move vowels", "\n";
		// O → I → A → U → E → D → C → T → N → NULL
		$sll->display();
	}
}
SingleLL::main(array());

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




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