Home > Article > Backend Development > PHP implements two-way linked list_PHP tutorial
I have been looking at the data structure for a long time but have never used it. I saw the data structure of PHP on the Internet, studied it and shared it with everyone. Last time I shared a linked list, this time I will add a few words about a doubly linked list. Short copy code without cutting no=$no; $this->name=$name; } static public function addHero($head,$hero) { $cur = $head; $isExist=false; //Judge the current linked list Is it empty if($cur->next==null) { $cur->next=$hero; $hero->pre=$cur; } else { //If it is not an empty node, arrange the name Add // Find the added position while($cur->next!=null) { if($cur->next->no > $hero->no) { break; } else if($cur ->next->no == $hero->no) { $isExist=true; echo "
The same number cannot be added"; } $cur=$cur->next; } if(!$isExist) { if($cur->next!=null) { $hero->next=$ cur->next; } $hero->pre=$cur; if($cur->next!=null) { $hero->next->pre=$hero; } $cur-> next=$hero; } } } //Traverse static public function showHero($head) { $cur=$head; while($cur->next!=null) { echo "
Number: ".$cur->next->no." Name: ".$cur->next->name; $cur=$cur->next; } } static public function delHero ($head,$herono) { $cur=$head; $isFind=false; while($cur!=null) { if($cur->no==$herono) { $isFind=true; break; } //Continue to find $cur=$cur->next; } if($isFind) { if($cur->next!=null) { $cur->next_pre=$cur->pre; } $ cur->pre->next=$cur->next; } else { echo "
Target not found"; } } } $head = new Hero(); $hero1 = new Hero(1,'1111'); $hero3 = new Hero(3,'3333'); $hero2 = new Hero (2,'2222'); Hero::addHero($head,$hero1); Hero::addHero($head,$hero3); Hero::addHero($head,$hero2); Hero::showHero($ head); Hero::delHero($head,2); Hero::showHero($head); ?>