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