PHP code PHP single linked list implementation code sharing

Source: Internet
Author: User
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.

  • 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.