'PHP, Implement a method of a class to append a linked list
I am trying to implement a method of a class to append a linked list of nodes received as an argument at the end of the last element of the invoking object. My code:
<?php
interface INode {
/**
* Set the data of the node's field
*
* @param mixed $data
*/
function setData($data);
/**
* Return the content of the data of this node
*
* @return mixed
*/
function getData();
/**
* Return the reference to the next node
*
* @return object
*/
function getNext();
} // interface INode
interface ILinkedList {
/**
* Return how many nodes are into the list
*
* @return int
*/
function getCount();
/**
* Insert a new node at the tail of the list
*
* @param mixed $item
*/
function push($item);
/**
* Append the given list to this
*
* @param LinkedList $linked_list
*/
function append(LinkedList $linked_list);
/**
* Print to video the link list as string like 1->3-> ...
*/
function printAsList();
} // interface ILinkedList
/**
* Class Node
*/
class Node implements INode {
/**
* The data of the node
*
* @var mixed
*/
private $_data;
/**
* The link to the next node
*
* @var object
*/
private $_next;
/**
* The constructor initialize attributes $_data and the link to the next node
*
* @param mixed $data
* @param object $next
*/
function __construct($data, $next = NULL)
{
$this->_data = $data;
$this->_next = $next;
}
function __toString()
{
return "{$this->_data}";
}
/**
* Set the data of the node's field
*
* @param mixed $data
*/
public function setData($data)
{
$this->_data = $data;
}
/**
* Set the reference link to the next node
*
* @param object $next
*/
protected function setNext($next)
{
$this->_next = $next;
}
/**
* Return the content of the data of this node
*
* @return mixed
*/
public function getData()
{
return $this->_data;
}
/**
* Return the reference to the next node
*
* @return object
*/
public function getNext()
{
return $this->_next;
}
} // class Node
/**
* Class LinkedList
*/
class LinkedList extends Node implements ILinkedList {
/**
* The pointer of the list
*
* @var object|NULL
*/
private $_head;
/**
* Counter of the nodes into the list
*
* @var int
*/
private $_count;
/**
* Initialize classe's attributes
*/
function __construct()
{
$this->_head = NULL;
$this->_count = 0;
}
/**
* Return the contents of the list as a string
*
* @return string
*/
function __toString()
{
return $this->printAsList();
}
/**
* Return how many nodes are into the list
*
* @return int
*/
public function getCount()
{
return $this->_count;
}
/**
* Insert a new node at the tail of the list
*
* @param mixed $item
*/
public function push($item) {
if ($this->_head !== NULL) {
// Scroll the list
/** @var Node $curr */
for($curr = $this->_head; $curr->getNext() !== NULL; $curr = $curr->getNext()) {}
// Updates the pointer of the last element to the new node
$curr->setNext(new Node($item));
}
else $this->_head = new Node($item);
$this->_count ++;
}
/**
* Append the given list to this
*
* @param LinkedList $linked_list
*/
public function append(LinkedList $linked_list)
{
if($linked_list === NULL) {
return FALSE;
}
/** @var Node $curr */
for($curr = $this->_head; $curr !== NULL; $curr = $curr->getNext()) {}
while($linked_list !== NULL) {
$curr->setNext(new Node($linked_list->getData())); // <------ Error here
$linked_list = $linked_list->getNext();
}
return TRUE;
}
/**
* Print to video the link list as string like 1->3-> ...
*/
public function printAsList()
{
$items = [];
/** @var Node $curr */
for($curr = $this->_head; $curr !== NULL; $curr = $curr->getNext()) {
echo $curr->getData() .'->';
}
}
} // class LinkedList
$a = new LinkedList;
$a->push(1);
$a->push(2);
$b = new LinkedList;
$b->push(5);
$b->push(6);
$a->append($b); // <---------------- This statement is the one with problems
?>
For example from the code, I would like after concatenating the object $a should be:
1->2->5->6
The last statement generates the error that I don't know how to correct.
Thanks in advance for any suggestions.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
