Skip to main content

Segregate even and odd nodes in sorted order in php

Php program for Segregate even and odd nodes in sorted order. Here problem description and other solutions.

<?php
// Php program for
// Segregate even and odd nodes in ascending order

// Linked list node
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class SingleLL
{
	public $head;
	public	function __construct()
	{
		$this->head = NULL;
	}
	// Add new node at the end of linked list
	public	function insert($value)
	{
		// Create  node
		$node = new LinkNode($value);
		if ($this->head == NULL)
		{
			$this->head = $node;
		}
		else
		{
			$temp = $this->head;
			// Find last node
			while ($temp->next != NULL)
			{
				// Visit to next node
				$temp = $temp->next;
			}
			// Add node at last position
			$temp->next = $node;
		}
	}
	// Display all Linked List elements
	public	function display()
	{
		if ($this->head != NULL)
		{
			$temp = $this->head;
			while ($temp != NULL)
			{
				// Display node value
				echo "  ",$temp->data;
				// Visit to next node
				$temp = $temp->next;
			}
		}
		else
		{
			echo "Empty Linked list\n";
		}
	}
	public	function sortedAdd($element)
	{
		if ($this->head == NULL)
		{
			$this->head = $element;
		}
		else if ($this->head->data >= $element->data)
		{
			$element->next = $this->head;
			$this->head = $element;
		}
		else
		{
			$temp = $this->head;
			// Finding location of inserting node
			while ($temp->next != NULL && 
                   $temp->next->data < $element->data)
			{
				// Visit to next node
				$temp = $temp->next;
			}
			// Add node 
			$element->next = $temp->next;
			$temp->next = $element;
		}
	}
	public	function segregateNode($odd, $even)
	{
		$node = NULL;
		// Iterating the linked list node
		while ($this->head != NULL)
		{
			$node = $this->head;
			// Visit to next node
			$this->head = $node->next;
			// Set null to next node
			$node->next = NULL;
			if ($node->data % 2 == 0)
			{
				// When node value is Even
				$even->sortedAdd($node);
			}
			else
			{
				// When node value is Odd
				$odd->sortedAdd($node);
			}
		}
	}
	public static
	function main($args)
	{
		// Create linked lists
		$sll = new SingleLL();
		$odd = new SingleLL();
		$even = new SingleLL();
		// Linked list sll
		// 4 → 3 → 5 → 2 → 11 → 1 → 6 → NULL
		$sll->insert(4);
		$sll->insert(3);
		$sll->insert(5);
		$sll->insert(2);
		$sll->insert(11);
		$sll->insert(1);
		$sll->insert(6);
		echo "Initial Element\n";
		// Display all node
		$sll->display();
		$sll->segregateNode($odd, $even);
		echo "\nEven Element\n";
		// Display all even node
		$even->display();
		echo "\nOdd Element\n";
		// Display all odd node
		$odd->display();
	}
}
SingleLL::main(array());

Output

Initial Element
  4  3  5  2  11  1  6
Even Element
  2  4  6
Odd Element
  1  3  5  11




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