John Davidson

Problem regarding Linklist in PHP. I'm traversing the data value in the linklist, i'm getting the duplicated values i do not know why

0 comments
Message:


when I run this code and trying to traverse the data field in Node it gives me duplicated values I do not know why. is anybody has any idea why I am getting these duplicated values...
Thanks in advance for your valuable input


<?php function linkedListMultiplierCheck( $arr, $init ) {
$head = new Node($arr[0],
new Node($arr[1],
new Node($arr[2],
new Node($arr[3], NULL))));

return linkedListMultiplier($head, $init);
}

class Node {
public $data, $next;
public function __construct($data, $next = NULL) {
list($this->data, $this->next) = [$data, $next];

}
}

// DO NOT MODIFY THE CODE ABOVE

function linkedListMultiplier($head, $init)
{
foreach ($head["data"] as $value) {
if($head["next"]==null){
$total = $total+1;
}
}

$t = $head;
$k=0;
$init =2;
while ($t->data){
$temp=$t->data;
print_r($temp . " ");
if (!$t->next) {
$t->next = new Node(0);
}

$t = $t->next;

};
return $head;
}

$head=linkedListMultiplierCheck([2, 3,4,5], 1);
linkedListMultiplier($head,1);
?>

when I run this code I am getting values like


2    3    4    5    2    3    4    5    

I do not know why it is duplicated the values it should only give me like the bellow


2    3    4    5


My Answer:
am facing an issue with traversing a linked list in PHP. I am unable to properly iterate through the nodes and access their data. Can you help me with this problem?

Rate this post

4 of 5 based on 1304 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us