リンクリストを実装するためにこのクラスを作成しました。
class Node{
public $data;
public $link;
function __construct($data, $next = NULL){
$this->data = $data;
$this->link = $next;
}
}
class CircularLinkedList{
private $first;
private $current;
private $count;
function __construct(){
$this->count = 0;
$this->first = null;
$this->current = null;
}
function isEmpty(){
return ($this->first == NULL);
}
function push($data){
//line 30
$p = new Node($data, $this->first);
if($this->isEmpty()){
$this->first = $p;
$this->current = $this->first;
}
else{
$q = $this->first;
//line 38
while($q->link != $this->first)
$q = $q->link;
$q->link = $p;
}
$this->count++;
}
function find($value){
$q = $this->first;
while($q->link != null){
if($q->data == $value)
$this->current = $q;
$q = $q->link;
}
return false;
}
function getNext(){
$result = $this->current->data;
$this->current = $this->current->link;
return $result;
}
}
しかし、私が何らかの価値を押し出そうとすると、
$ll = new CircularLinkedList();
$ll->push(5);
$ll->push(6);
$ll->push(7);
$ll->push(8);
$ll->push(9);
$ll->push(10);
//$ll->find(7);
for($j=0;$j<=30;$j++){
$result = $ll->getNext();
echo $result."<br />";
}
スクリプトは2回目のプッシュでハングアウトし、max_execution_time
エラーが発生します。
通常のLinkedListとして、上記のようにcalsの2行30と38を変更すると、正常に機能します。(最初のノードへの最後のノードリンクを削除することによって)。
それで、問題は何ですか、そしてそれをどのように解決するのですか?
更新:関数をこれに変更することによりpush()
、線形リンクリストとして正常に機能します:
function push($data){
$p = new Node($data);
if($this->isEmpty()){
$this->first = $p;
$this->current = $this->first;
}
else{
$q = $this->first;
while($q->link != null)
$q = $q->link;
$q->link = $p;
}
$this->count++;
}