Skip to main content

Delete all nodes whose node digit sum is odd from circular linked list in php

Php program for Delete all nodes whose node digit sum is odd from circular linked list. Here problem description and explanation.

<?php
// Php Program
// Remove all the Odd digit sum nodes from a circular linked list
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class CircularLinkedList
{
	public $head;
	public $tail;
	public	function __construct()
	{
		$this->head = NULL;
		$this->tail = NULL;
	}
	//Add node at the end position efficiently
	public	function insert($value)
	{
		// Create a new node
		$node = new LinkNode($value);
		if ($this->head == NULL)
		{
			// When first node
			$this->head = $node;
		}
		else
		{
			// Connect new node at the last
			$this->tail->next = $node;
		}
		// Connect last node to first node
		$node->next = $this->head;
		// Set new last node
		$this->tail = $node;
	}
	// Display node element of circular linked list
	public	function display()
	{
		if ($this->head == NULL)
		{
			echo "\n Empty Linked List\n";
		}
		else
		{
			// First node of linked list
			echo " ",$this->head->data;
			$temp = $this->head->next;
			// Display linked list node
			while ($temp != NULL && $temp != $this->head)
			{
				// Display node value
				echo "  ",$temp->data;
				// visit to next node
				$temp = $temp->next;
			}
		}
	}
	// Sum of digits
	public	function digitSum($num)
	{
		$n = $num;
		$sum = 0;
		if ($n < 0)
		{
			// When node value is negative
			$n = -$n;
		}
		while ($n > 0)
		{
			$sum += ($n % 10);
			$n = (int)($n / 10);
		}
		return $sum;
	}
	public	function removeOddDigitSumNode()
	{
		if ($this->head == NULL)
		{
			return;
		}
		$temp = $this->head;
		$auxiliary = $this->head;
		$point = NULL;
		while ($temp != NULL)
		{
			if (($this->digitSum($temp->data) % 2) != 0)
			{
				$auxiliary = $temp;
				$temp = $temp->next;
				if ($auxiliary == $this->head)
				{
					// Enter to remove first node
					if ($auxiliary == $this->tail)
					{
						// Enter to removing single node
						$this->tail = NULL;
						$this->head = NULL;
						$temp = NULL;
						$point = NULL;
					}
					else
					{
						$this->head = $temp;
						$this->tail->next = $temp;
					}
				}
				else if ($auxiliary == $this->tail)
				{
					// When Remove last node
					if ($point != NULL)
					{
						$point->next = $this->head;
					}
					$this->tail = $point;
				}
				else
				{
					// When removing intermediate node
					if ($point != NULL)
					{
						$point->next = $temp;
					}
				}
				$auxiliary = NULL;
			}
			else
			{
				$point = $temp;
				// Visit to next node
				$temp = $temp->next;
				if ($temp == $this->head)
				{
					// Stop the process
					$temp = NULL;
				}
			}
		}
	}
	public static
	function main($args)
	{
		$cll = new CircularLinkedList();
		// Insert node
		$cll->insert(131);
		$cll->insert(12);
		$cll->insert(33);
		$cll->insert(10);
		$cll->insert(143);
		$cll->insert(91);
		$cll->insert(27);
		$cll->insert(125);
		$cll->insert(89);
		echo "\n Before remove odd digit sum nodes", "\n";
		$cll->display();
		/*
		    Node   Digit Sum    Odd
		    ----- -----------  --------
		    131   [1+3+1] 5     Yes
		    12    [1+2]   3     Yes
		    33    [3+3]   6     No
		    10    [10]    1     Yes
		    143   [1+4+3] 8     No
		    91    [9+1]   10    No
		    27    [2+7]   9     Yes        
		    125   [1+2+5] 8     No
		    89    [8+9]   17    Yes
		    --------------------------
		    Result 
		    ------
		    33 → 143 → 91 → 125 ┐ 
		     └-----------------⤶
		        
		*/
		$cll->removeOddDigitSumNode();
		echo "\n After remove odd digit sum nodes\n";
		$cll->display();
	}
}
CircularLinkedList::main(array());

Output

 Before remove odd digit sum nodes
 131  12  33  10  143  91  27  125  89
 After remove odd digit sum nodes
 33  143  91  125




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