php 循环链表,PHP实现循环链表功能
這篇文章主要介紹了PHP簡單實現(xiàn)循環(huán)鏈表功能,簡單描述了循環(huán)鏈表的概念、功能并結合實例形式分析了php定義及使用循環(huán)鏈表的相關操作技巧,需要的朋友可以參考下
具體如下:
概述:
循環(huán)鏈表是另一種形式的鏈式存貯結構。它的特點是表中最后一個結點的指針域指向頭結點,整個鏈表形成一個環(huán)。
如下圖所示:
實現(xiàn)代碼:
class node{
public $data;
public $link;
public function __construct($data=null,$link=null){
$this->data=$data;
$this->link=$link;
}
}
class cycleLinkList{
public $head;
public function __construct($data,$link=null){
$this->head=new node($data,$link);
$this->head->link=$this->head;
}
public function insertLink($data){
$p=new node($data);
$q=$this->head->link;
$r=$this->head;
if($q==$r)
{
$q->link=$p;
$p->link=$q;
return;
}
while($q!=$this->head){
$r=$q;$q=$q->link;
}
$r->link=$p;
$p->link=$this->head;
}
}
$linklist=new cycleLinkList(1);
for($i=2;$i<11;$i++){
$linklist->insertLink($i);
}
$q=$linklist->head->link;
echo $linklist->head->data;
while($q!=$linklist->head){
echo $q->data;
$q=$q->link;
}
echo "
--------------------------
";
$p=$linklist->head;
$r=$p;
$n=10;
$i=2;
while($n)
{
while(0!=$i){
$r=$p;$p=$p->link;
$i--;
}
echo $p->data;
$r->link=$p->link;
$tmp=$p;
$p=$p->link;
unset($tmp);
$n--;
$i=2;
}
?>
運行結果:
12345678910
--------------------------
36927185104
相關推薦:
JavaScript數(shù)據(jù)結構之單鏈表和循環(huán)鏈表實例分享
PHP簡單實現(xiàn)循環(huán)鏈表功能示例
JavaScript雙向鏈表和雙向循環(huán)鏈表的實現(xiàn)
總結
以上是生活随笔為你收集整理的php 循环链表,PHP实现循环链表功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab涡轮机建模,数学实验大作业-
- 下一篇: oracle 格式化报表输出,perl的