This example for everyone to share the PHP single-linked list of specific code for your reference, the specific content as follows
<?php/** * Single-linked list */class demo{private $id; Public $name; Public $next; Public function __construct ($id = ', $name = ') {$this->id = $id; $this->name = $name; The static public function show ($head) {$cur = $head; while ($cur->next) {echo $cur->next->id, ' # # # ', $cur->next->name, '
'; $cur = $cur->next; } Echo '
'; }//tail plug method static public function push ($head, $node) {$cur = $head; while (NULL! = $cur->next) {$cur = $cur->next; } $cur->next = $node; return $head; } static Public Function Insert ($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 Function edit ($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 Function pop ($head, $node) {$cur = $head; while (NULL! = $cur->next) {if ($cur->next = = $node) {break; } $cur = $cur->next; } $cur->next = $node->next; return $head; }} $team = NEW demo (); $node 1 = new Demo (1, ' Don Sanzang ');D emo::p ush ($team, $node 1); $node 1->name = ' Tang Monk ';D emo::show ($team);//Demo::show ($ Team); $node 2 = new Demo (2, ' Monkey King ');D Emo::insert ($team, $node 2);//Demo::show ($team); $node 3 = new Demo (5, ' White Dragon Horse ');D emo:: Push ($team, $node 3);//Demo::show ($team); $node 4 = new Demo (3, ' pig ');D Emo::insert ($team, $node 4);//Demo::show ($team); $node 5 = new Demo (4, ' Sand Monk ');D Emo::insert ($team, $node 5);//Demo::show ($team); $node 4->name = ' pig enlightenment ';//php object is referenced, so the demo :: Edit not necessary//unset ($node 4);//$node 4 = new Demo (3, ' Pig Awareness Energy ');//Demo::edit ($team, $node 4);D Emo::p op ($team, $node 1);D emo:: Show ($team);
The above is the whole content of this article, I hope that everyone to implement the PHP single-linked list is helpful.
The above describes the PHP code PHP single linked list implementation code sharing, including the contents of the PHP code, I hope that the PHP tutorial interested in a friend helpful.