PHP realize bidirectional linked list

Source: Internet
Author: User
Tags add empty
Read a long time data structure but not how to use, on the Internet to see the data on the structure of PHP, learn a bit, and share with you. Last time I shared a list, this time to add a two-way list.   Brief no cut                               &NBSP ;                                   &NB Sp                                   &NB Sp                           Copy code <?php     &NB Sp   Class Hero         {            public $pre =null             public $no;             public $name;             public $next =null;                          Public function __construct ($no = ", $name = ')   &NBSp         {                $this->no= $no     &NBS P           $this->name= $name;                                  &N Bsp   Static public Function Addhero ($head, $hero)             {                $cur = $head;                 $isExist =false;                /Determine if the current list is empty               &N Bsp if ($cur->next==null)                 {          &NBSP ;         $cur->next= $hero;                     $hero->pre= $cur;                 &NBsp               else                 {  &NB Sp                //If not an empty node, schedule name to add                    //Find added location                                          while ($cur->next!=null)           &NBS P         {                        if ($cur- >next->no > $hero->no)                         {& nbsp                           break;                                   & nbsp       &NBSp     Else if ($cur->next->no = $hero->no)                         {                            $isExist =true;                             echo "<br> cannot add the same edit Number ";                                   & nbsp             $cur = $cur->next;                                   & nbsp     if (! $isExist)                     {      & nbsp                 if ($cur->next!=null)                         {                             $hero->next= $cur ; next;                                   & nbsp             $hero->pre= $cur;                         if ($cur->next!=null)                         {              & nbsp             $hero->next->pre= $hero;                                   & nbsp             $cur->next= $hero;                                   &NB Sp    }       &NBSP;        }                                     //traversal             static public function Showhero ($ Head)             {                $cur = $head; &nbs P               while ($cur->next!=null)             &NBS P   {                    echo "<br> No.:" $cur->next->no. " Name: ". $cur->next->name;                     $cur = $cur->next;                            }                          static public function Delhero ($head, $herono)       &NBSP     {                $cur = $head;           &NB Sp     $isFind =false;                 while ($cur!=null)                 {                    if ($cur->no== $herono)     &NB Sp               {                    & nbsp   $isFind =true;                         break;                                   & nbsp    //continue to find                     $cur = $cur->next;                                 IF($isFind)                 {              &NBSP ;     if ($cur->next!=null)                     {  &NBSP ;                     $cur->next_pre= $cur->pre;                                   & nbsp     $cur->pre->next= $cur->next;                                 Else &NB Sp               {                    E Cho "<br> did not find the target";                                   & nbsp        }         &NBS  P       $head = new Hero ();         $hero 1 = new Hero (1, ' 1111 ');         $hero 3 = new Hero (3, ' 3333 ');         $hero 2 = new Hero (2, ' 2222 ');         Hero::addhero ($head, $hero 1);         Hero::addhero ($head, $hero 3);         Hero::addhero ($head, $hero 2);         Hero::showhero ($head);         Hero::d Elhero ($head, 2);         Hero::showhero ($head);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.