PHP implements a doubly linked list

Source: Internet
Author: User

Read a long time data structure but not how to use, in the Internet to see the data structure about PHP, learning a bit, and share with you. The last time I shared a linked list, this time to add a doubly linked list.

short and not cut

<?PHPclassHero { Public $pre=NULL;  Public $no;  Public $name;  Public $next=NULL;  Public function__construct ($no= ",$name= ' ')            {                $this->no=$no; $this->name=$name; }                        Static  Public functionAddhero ($head,$hero)            {                $cur=$head; $isExist=false; //determine if the current list is empty                if($cur-Next==NULL)                {                    $cur-Next=$hero; $hero->pre=$cur; }                Else                {                    //if it is not an empty node, arrange the name to add//Find the added location                                         while($cur-Next!=NULL)                    {                        if($cur-Next->no >$hero-NO) {                             Break; }                        Else if($cur-Next->no = =$hero-NO) {                            $isExist=true; Echo"<br> cannot add the same number"; }                        $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 functionShowhero ($head)            {                $cur=$head;  while($cur-Next!=NULL)                {                    Echo"<br> number:".$cur-Next->no. " Name: ".$cur-Next-name; $cur=$cur-Next; }            }                        Static  Public functionDelhero ($head,$herono)            {                $cur=$head; $isFind=false;  while($cur!=NULL)                {                    if($cur->no==$herono)                    {                        $isFind=true;  Break; }                    //Keep looking .                    $cur=$cur-Next; }                if($isFind)                {                    if($cur-Next!=NULL)                    {                        $cur->next_pre=$cur-Pre; }                    $cur->pre->Next=$cur-Next; }                Else                {                    Echo"<br> no target Found"; }                            }        }        $head=NewHero (); $hero 1=NewHero (1, ' 1111 '); $hero 3=NewHero (3, ' 3333 '); $hero 2=NewHero (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);?>

I'm the dividing line of the king of the Land Tiger.

"PHP implementation linked list" Portal: http://www.cnblogs.com/yydcdut/p/3777760.html

Reprint Please specify source: Http://www.cnblogs.com/yydcdut

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.