我正在創建鏈接列表類,並且對引用對象有一些困惑。PHP:引用對象的鏈表實現問題?
按照我的理解默認情況下,對象是通過引用複製的。 $ Obj1 = $ Obj2。 $ Obj1是$ Obj2的別名。
有人能指出哪一個在鏈表實現中是正確的。
$firstNode->next = $this->first;---> seems to be correct
or
$firstNode->next =& $this->first;
$this->first = $firstNode;-----> seems to be correct as $firstNode is an object
or
$this->first = & $firstNode;
代碼:
class Node {
public $element;
public $next;
public function __construct($element){
$this->element = $element;
$this->next = NULL;
}
}
class Linklist {
private $first;
private $listSize;
public function __construct(){
$this->first = NULL;
$this->listSize = 0;
}
public function InsertToFirst($element){
$firstNode = new Node($element);
$firstNode->next = $this->first; // or $firstNode->next =& $this->first;
$this->first = $firstNode; // or $this->first = & $firstNode;
}
(如果這不是作業)爲什麼你會在PHP中創建一個鏈表? – erenon 2012-01-17 15:09:34