看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下。上一次分享了《PHP小教程之实现链表》,这次来补充说一下双向链表。
代码如下:
404_5@PHP@H_404_5@ class Hero@H_404_5@ {@H_404_5@ public $pre=null;@H_404_5@ public $no;@H_404_5@ public $name;@H_404_5@ public $next=null;@H_404_5@ public function __construct($no='',$name='')@H_404_5@ {@H_404_5@ $this->no=$no;@H_404_5@ $this->name=$name;@H_404_5@ }@H_404_5@ static public function addHero($head,$hero)@H_404_5@ {@H_404_5@ $cur = $head;@H_404_5@ $isExist=false;@H_404_5@ //判断目前这个链表是否为空@H_404_5@ if($cur->next==null)@H_404_5@ {@H_404_5@ $cur->next=$hero;@H_404_5@ $hero->pre=$cur;@H_404_5@ }@H_404_5@ else@H_404_5@ {@H_404_5@ //如果不是空节点,则安排名来添加@H_404_5@ //找到添加的位置@H_404_5@ while($cur->next!=null)@H_404_5@ {@H_404_5@ if($cur->next->no > $hero->no)@H_404_5@ {@H_404_5@ break;@H_404_5@ }@H_404_5@ else if($cur->next->no == $hero->no)@H_404_5@ {@H_404_5@ $isExist=true;@H_404_5@ echo "
不能添加相同的编号";@H_404_5@ }@H_404_5@ $cur=$cur->next;@H_404_5@ }@H_404_5@ if(!$isExist)@H_404_5@ {@H_404_5@ if($cur->next!=null)@H_404_5@ {@H_404_5@ $hero->next=$cur->next;@H_404_5@ }@H_404_5@ $hero->pre=$cur;@H_404_5@ if($cur->next!=null)@H_404_5@ {@H_404_5@ $hero->next->pre=$hero;@H_404_5@ }@H_404_5@ $cur->next=$hero; @H_404_5@ }@H_404_5@ }@H_404_5@ }@H_404_5@ //遍历@H_404_5@ static public function showHero($head)@H_404_5@ {@H_404_5@ $cur=$head;@H_404_5@ while($cur->next!=null)@H_404_5@ {@H_404_5@ echo "
编号:".$cur->next->no."名字:".$cur->next->name;@H_404_5@ $cur=$cur->next;@H_404_5@ }@H_404_5@ }@H_404_5@ static public function delHero($head,$herono)@H_404_5@ {@H_404_5@ $cur=$head;@H_404_5@ $isFind=false;@H_404_5@ while($cur!=null)@H_404_5@ {@H_404_5@ if($cur->no==$herono)@H_404_5@ {@H_404_5@ $isFind=true;@H_404_5@ break;@H_404_5@ }@H_404_5@ //继续找@H_404_5@ $cur=$cur->next;@H_404_5@ }@H_404_5@ if($isFind)@H_404_5@ {@H_404_5@ if($cur->next!=null)@H_404_5@ {@H_404_5@ $cur->next_pre=$cur->pre;@H_404_5@ }@H_404_5@ $cur->pre->next=$cur->next;@H_404_5@ }@H_404_5@ else@H_404_5@ {@H_404_5@ echo "
没有找到目标";@H_404_5@ } @H_404_5@ }@H_404_5@ }@H_404_5@ $head = new Hero();@H_404_5@ $hero1 = new Hero(1,'1111');@H_404_5@ $hero3 = new Hero(3,'3333');@H_404_5@ $hero2 = new Hero(2,'2222');@H_404_5@ Hero::addHero($head,$hero1);@H_404_5@ Hero::addHero($head,$hero3);@H_404_5@ Hero::addHero($head,$hero2);@H_404_5@ Hero::showHero($head);@H_404_5@ Hero::delHero($head,2);@H_404_5@ Hero::showHero($head);@H_404_5@?>@H_404_5@
不能添加相同的编号";@H_404_5@ }@H_404_5@ $cur=$cur->next;@H_404_5@ }@H_404_5@ if(!$isExist)@H_404_5@ {@H_404_5@ if($cur->next!=null)@H_404_5@ {@H_404_5@ $hero->next=$cur->next;@H_404_5@ }@H_404_5@ $hero->pre=$cur;@H_404_5@ if($cur->next!=null)@H_404_5@ {@H_404_5@ $hero->next->pre=$hero;@H_404_5@ }@H_404_5@ $cur->next=$hero; @H_404_5@ }@H_404_5@ }@H_404_5@ }@H_404_5@ //遍历@H_404_5@ static public function showHero($head)@H_404_5@ {@H_404_5@ $cur=$head;@H_404_5@ while($cur->next!=null)@H_404_5@ {@H_404_5@ echo "
编号:".$cur->next->no."名字:".$cur->next->name;@H_404_5@ $cur=$cur->next;@H_404_5@ }@H_404_5@ }@H_404_5@ static public function delHero($head,$herono)@H_404_5@ {@H_404_5@ $cur=$head;@H_404_5@ $isFind=false;@H_404_5@ while($cur!=null)@H_404_5@ {@H_404_5@ if($cur->no==$herono)@H_404_5@ {@H_404_5@ $isFind=true;@H_404_5@ break;@H_404_5@ }@H_404_5@ //继续找@H_404_5@ $cur=$cur->next;@H_404_5@ }@H_404_5@ if($isFind)@H_404_5@ {@H_404_5@ if($cur->next!=null)@H_404_5@ {@H_404_5@ $cur->next_pre=$cur->pre;@H_404_5@ }@H_404_5@ $cur->pre->next=$cur->next;@H_404_5@ }@H_404_5@ else@H_404_5@ {@H_404_5@ echo "
没有找到目标";@H_404_5@ } @H_404_5@ }@H_404_5@ }@H_404_5@ $head = new Hero();@H_404_5@ $hero1 = new Hero(1,'1111');@H_404_5@ $hero3 = new Hero(3,'3333');@H_404_5@ $hero2 = new Hero(2,'2222');@H_404_5@ Hero::addHero($head,$hero1);@H_404_5@ Hero::addHero($head,$hero3);@H_404_5@ Hero::addHero($head,$hero2);@H_404_5@ Hero::showHero($head);@H_404_5@ Hero::delHero($head,2);@H_404_5@ Hero::showHero($head);@H_404_5@?>@H_404_5@