Analog Linked list:
<?php/** * PHP implements the basic operation of the linked list */class linklist {/** * name * @var String */public $name = '; /** * number * @var int */Public $id = 0; /* * Reference Next Object */public $next = null; /** * Constructor Initialization data * @param int $id * @param string $name */Public function __construct ($id = 0, $name = ') {$ This->name = $name; $this->id = $id; }/** * Traverse list */public static function Echo_link_list ($head) {$curr = $head; while ($curr->next! = null) {echo ' name: '. $curr->next->name, ' number: '. $curr->next->id; Echo '
'; $curr = $curr->next; }}/** * Add new node */public static function Add ($head, $id, $name) {$curr = $head; $obj = new Linklist ($id, $name); while ($curr->next! = null) {//If the current ID < Next ID is added to the middle, add the node to the specified order position if ($curr->next->id > $id) { $obj->next = $curr->next; $curr->next = $obj; return true; } else if ($curr->next->id = = $id) {echo ' current ID: '. $id. ' Repeat, please do not continue adding! '; Echo '
'; return false; } $curr = $curr->next; }//Add node to tail if ($curr->next = = null) {$curr->next = $obj; }}/** * Delete node */public static function del ($head, $id) {$curr = $head; while ($curr->next! = null) {if ($curr->next->id = = $id) {$curr->next = $curr->next->next; return true; } $curr = $curr->next; }}/** * Modify node */public static function edit ($head, $id, $new _name) {$curr = $head; while ($curr->next! = null) {if ($curr->next->id = = $id) {$curr->next->name = $new _name; } $curr = $curr->next; }}} $head = new linklist (); Linklist::add ($head, 1, ' wangdk '); Linklist::add ($head, 2, ' sunshuzhen '); Linklist::add ($head , 8, ' Wanghaha '); Linklist::add ($head, 6, ' Wangchufen '); Linklist::add ($head, 6, ' Wangchufen '); Linklist::add ($head, 3, ' Wangdaye '); linklist::d El ($head, 1); Linklist::edit ($head, 2, ' hahaha ');Linklist::echo_link_list ($head);?>
List of additions and deletions to check and change:
<?php/** * PHP implements the basic operation of the linked list */class linklist {/** * name * @var String */public $name = '; /** * number * @var int */Public $id = 0; /* * Reference Next Object */public $next = null; /** * Constructor Initialization data * @param int $id * @param string $name */Public function __construct ($id = 0, $name = ') {$ This->name = $name; $this->id = $id; }/** * Traverse list */public static function Echo_link_list ($head) {$curr = $head; while ($curr->next! = null) {echo ' name: '. $curr->next->name, ' number: '. $curr->next->id; Echo '
'; $curr = $curr->next; }}/** * Add new node */public static function Add ($head, $id, $name) {$curr = $head; $obj = new Linklist ($id, $name); while ($curr->next! = null) {//If the current ID < Next ID is added to the middle, add the node to the specified order position if ($curr->next->id > $id) { $obj->next = $curr->next; $curr->next = $obj; return true; } else if ($curr->next->id = = $id) {echo ' current ID: '. $id. ' Repeat, please do not continue adding! '; Echo '
'; return false; } $curr = $curr->next; }//Add node to tail if ($curr->next = = null) {$curr->next = $obj; }}/** * Delete node */public static function del ($head, $id) {$curr = $head; while ($curr->next! = null) {if ($curr->next->id = = $id) {$curr->next = $curr->next->next; return true; } $curr = $curr->next; }}/** * Modify node */public static function edit ($head, $id, $new _name) {$curr = $head; while ($curr->next! = null) {if ($curr->next->id = = $id) {$curr->next->name = $new _name; } $curr = $curr->next; }}} $head = new linklist (); Linklist::add ($head, 1, ' wangdk '); Linklist::add ($head, 2, ' sunshuzhen '); Linklist::add ($head , 8, ' Wanghaha '); Linklist::add ($head, 6, ' Wangchufen '); Linklist::add ($head, 6, ' Wangchufen '); Linklist::add ($head, 3, ' Wangdaye '); linklist::d El ($head, 1); Linklist::edit ($head, 2, ' hahaha ');Linklist::echo_link_list ($head);?>