Basic operations examples for simulating linked lists and lists in PHP, basic operations Example _php tutorial

Source: Internet
Author: User

Basic operations examples for simulating linked lists and linked lists in PHP, basic operations examples


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);?>

Articles you may be interested in:

    • PHP List Usage Example analysis
    • PHP doubly linked list (spldoublylinkedlist) Introduction and usage examples
    • PHP Mini-tutorial implementation of a doubly linked list
    • PHP Small tutorial Implementation linked list
    • PHP implementation of single-linked list of instance code

http://www.bkjia.com/PHPjc/1104328.html www.bkjia.com true http://www.bkjia.com/PHPjc/1104328.html techarticle Basic Operation example of the list and chain list in PHP, basic Operation example Simulation list: PHP/** * PHP Implementation of the basic operation of the linked list */class linklist {/** * name * @var String */publ ...

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