Posted on by Kalkicode

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
{
public \$data;
public \$next;
public	function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
{
public \$tail;
public	function __construct()
{
\$this->tail = NULL;
}
//Add node at the end position efficiently
public	function insert(\$value)
{
// Create a new node
{
// When first node
}
else
{
// Connect new node at the last
\$this->tail->next = \$node;
}
// Connect last node to first node
// Set new last node
\$this->tail = \$node;
}
// Display node element of circular linked list
public	function display()
{
{
}
else
{
// First node of linked list
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()
{
{
return;
}
\$point = NULL;
while (\$temp != NULL)
{
if ((\$this->digitSum(\$temp->data) % 2) != 0)
{
\$auxiliary = \$temp;
\$temp = \$temp->next;
{
// Enter to remove first node
if (\$auxiliary == \$this->tail)
{
// Enter to removing single node
\$this->tail = NULL;
\$temp = NULL;
\$point = NULL;
}
else
{
\$this->tail->next = \$temp;
}
}
else if (\$auxiliary == \$this->tail)
{
// When Remove last node
if (\$point != NULL)
{
}
\$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;
{
// Stop the process
\$temp = NULL;
}
}
}
}
public static
function main(\$args)
{
// 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();
}
}

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.

Categories
Relative Post