Skip to main content

Organize linked list into groups of even and odd nodes in php

Php program for Organize linked list into groups of even and odd nodes. Here problem description and explanation.

<?php
/*
    Php program for
    Grouping of even and odd nodes in 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	function __construct()
	{
		$this->head = NULL;
	}
	// Adding new node at beginning of linked list
	public	function insert($data)
	{
		// Create new node
		$node = new LinkNode($data);
		// Connect current node to head
		$node->next = $this->head;
		$this->head = $node;
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == NULL)
		{
			return;
		}
		$temp = $this->head;
		// iterating linked list elements
		while ($temp != NULL)
		{
			echo " $temp->data →";
			// Visit to next node
			$temp = $temp->next;
		}
		echo " NULL\n";
	}
	// Arrange alternative even and odd nodes in linked list
	public	function arrangeEvenOdds()
	{
		// Define some auxiliary variables
		$even_node = NULL;
		$odd_node = NULL;
		$tail_1 = NULL;
		$tail_2 = NULL;
		$auxiliary = $this->head;
		$this->head = NULL;
		// Segregation of even and odd nodes elements
		while ($auxiliary != NULL)
		{
			if ($auxiliary->data % 2 == 0)
			{
				// Even Node element
				if ($even_node == NULL)
				{
					$even_node = $auxiliary;
				}
				else
				{
					// Add node at end of even list
					$tail_1->next = $auxiliary;
				}
				// Get new last node
				$tail_1 = $auxiliary;
				// visit to next node
				$auxiliary = $auxiliary->next;
				// set next node is null
				$tail_1->next = NULL;
			}
			else
			{
				// Odd Node element
				if ($odd_node == NULL)
				{
					$odd_node = $auxiliary;
				}
				else
				{
					// Add node at end of odd list
					$tail_2->next = $auxiliary;
				}
				// Get new last node
				$tail_2 = $auxiliary;
				// visit to next node
				$auxiliary = $auxiliary->next;
				// set next node is null
				$tail_2->next = NULL;
			}
		}
		if ($even_node != NULL)
		{
			// new first node of linked list
			$this->head = $even_node;
		}
		else
		{
			// When only odd element exists in linked list
			$this->head = $odd_node;
		}
		// Combine even and odd nodes
		while ($even_node != NULL && $odd_node != NULL)
		{
			// next node of even list
			$tail_1 = $even_node->next;
			// next node of odd list
			$tail_2 = $odd_node->next;
			$even_node->next = $odd_node;
			if ($tail_1 != NULL)
			{
				// This is useful to handle 
				// when even linked list next node is empty 
				// and odd elements are next node not empty.
				$odd_node->next = $tail_1;
			}
			// Visit to next node
			$even_node = $tail_1;
			$odd_node = $tail_2;
		}
	}
	public static
	function main($args)
	{
		$list1 = new SingleLL();
		$list2 = new SingleLL();
		$list3 = new SingleLL();
		// Create linked list1
		$list1->insert(8);
		$list1->insert(2);
		$list1->insert(9);
		$list1->insert(7);
		$list1->insert(3);
		$list1->insert(10);
		$list1->insert(5);
		$list1->insert(4);
		$list1->insert(6);
		// Create linked list2
		$list2->insert(1);
		$list2->insert(1);
		$list2->insert(4);
		$list2->insert(2);
		$list2->insert(3);
		$list2->insert(1);
		// Create linked list3
		$list3->insert(2);
		$list3->insert(3);
		$list3->insert(3);
		$list3->insert(8);
		$list3->insert(4);
		$list3->insert(6);
		$list3->insert(2);
		// Test A
		echo " Before arrange : \n";
		// Before arrange nodes
		$list1->display();
		// Case A
		// When linked list contains same length of 
		// Even and Odd nodes
		$list1->arrangeEvenOdds();
		echo " After arrange  : \n";
		// After arrange nodes
		$list1->display();
		// Test B
		echo "\n Before arrange : \n";
		// Before arrange nodes
		$list2->display();
		// Case B
		// When linked list Odd nodes are more than Even
		$list2->arrangeEvenOdds();
		echo " After arrange  : \n";
		// After arrange nodes
		$list2->display();
		// Test C
		echo "\n Before arrange : \n";
		// Before arrange nodes
		$list3->display();
		// Case C
		// When linked list Odd nodes are more than Even
		$list3->arrangeEvenOdds();
		echo " After arrange  : \n";
		// After arrange nodes
		$list3->display();
	}
}
SingleLL::main(array());

Output

 Before arrange :
 6 → 4 → 5 → 10 → 3 → 7 → 9 → 2 → 8 → NULL
 After arrange  :
 6 → 5 → 4 → 3 → 10 → 7 → 2 → 9 → 8 → NULL

 Before arrange :
 1 → 3 → 2 → 4 → 1 → 1 → NULL
 After arrange  :
 2 → 1 → 4 → 3 → 1 → 1 → NULL

 Before arrange :
 2 → 6 → 4 → 8 → 3 → 3 → 2 → NULL
 After arrange  :
 2 → 3 → 6 → 3 → 4 → 8 → 2 → 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