PHP implementation of single-linked list, PHP implementation of single-chain
Php/** * Single linked list*/ classdemo{Private $id; Public $name; Public $next; Public function__construct ($id= '',$name= '') { $this->id =$id; $this->name =$name; } Static Public functionShow ($head) { $cur=$head; while($cur-Next) { Echo $cur-Next->id, ' # # # ',$cur-Next->name, '
'; $cur=$cur-Next; } Echo'
'; } //tail interpolation method Static Public functionPush ($head,$node) { $cur=$head; while(NULL!=$cur-Next) { $cur=$cur-Next; } $cur-Next=$node; return $head; } Static Public functionInsert$head,$node) { $cur=$head; while(NULL!=$cur-Next) { if($cur-Next->id >$node-ID) { Break; } $cur=$cur-Next; } $node-Next=$cur-Next; $cur-Next=$node; return $head; } Static Public functionEdit$head,$node) { $cur=$head; while(NULL!=$cur-Next) { if($cur-Next->id = =$node-ID) { Break; } $cur=$cur-Next; } $cur-Next->name =$node-name; return $head; } Static Public functionPop ($head,$node) { $cur=$head; while(NULL!=$cur-Next) { if($cur-Next==$node) { Break; } $cur=$cur-Next; } $cur-Next=$node-Next; return $head; }}$team=NewDemo ();$node 1=NewDemo (1, ' Don Sanzang ');D Emo::p Ush ($team,$node 1);$node 1->name = ' Tang Monk ';D Emo:: Show ($team);//demo::show ($team);$node 2=NewDemo (2, ' Monkey King '));D Emo:: Insert ($team,$node 2);//demo::show ($team);$node 3=NewDemo (5, ' White Dragon Horse ');D Emo::p Ush ($team,$node 3);//demo::show ($team);$node 4=NewDemo (3, ' pig ');D Emo:: Insert ($team,$node 4);//demo::show ($team);$node 5=NewDemo (4, ' Sand monk ');D Emo:: Insert ($team,$node 5);//demo::show ($team);$node 4->name = ' pig awareness can ';//PHP object is referenced, so demo::edit is not necessary//unset ($node 4);//$node 4 = new Demo (3, ' Pig Awareness Energy ');//Demo::edit ($team, $node 4);Demo::p op ($team,$node 1);D Emo:: Show ($team);
http://www.bkjia.com/PHPjc/1139360.html www.bkjia.com true http://www.bkjia.com/PHPjc/1139360.html techarticle PHP implementation of single-linked list, PHP implementation of single-chain, PHP/* * Single-linked list */class demo{private $id, public $name, public $next, public function __constru CT ($id = ', $name = ') {$this ...