How PHP implements a doubly linked list and sorts

Source: Internet
Author: User
A doubly linked list is also called a doubly linked list, which is a list of two pointers in each data node, pointing directly to successive and direct precursors respectively. So, starting from any node in a doubly linked list, it is easy to access its predecessor and successor nodes.

<?php/** * Two-way list implementation User Leaderboard * * Only to embody the thought logic, does not have the actual reference value * @author crazy old driver * @date 2016-07-07 * * * class rank{/** * @var    Reference to the previous node */public $pre = null;    /** * @var A reference to the latter node */public $next = null;    /** * @var user Rank ID */public $id;          /** * @var User name */public $username;        Public function __construct ($id = ', $username = ') {$this->id = $id;    $this->username = $username; }/** * Add member Node method * * @access public * @param obj Head initial node * @param obj Rank member node */public static function Addrank ($head, $rank) {$cur = $head;//secondary node $isExist = false;//This is a flag bit while ($cur-&            Gt;next! = null) {if ($cur->next->id > $rank->id) {break;                }else if ($cur->next->id = = $rank->id) {$isExist = true;              Echo ' <br/> cannot add the same ID ';          } $cur = $cur->next; } if(! $isExist)              {if ($cur->next! = null) {$rank->next = $cur->next;              } $rank->pre = $cur;              if ($cur->next! = null) {$cur->next->pre = $rank;          } $cur->next = $rank;     }}/** * Delete member Node method * * @access public * @param obj Head initial node * @param obj rankid user rank ID        */public static function Delrank ($head, $rankid) {$cur = $head->next; $isFind = flase;                Mark bit while ($cur! = null) {if ($cur->id = = $rankid) {$isFind = true;            Break        } $cur = $cur->next;            if ($isFind) {if ($cur->next! = null) {$cur->next->pre = $cur->pre;            } $cur->pre->next = $cur->next;        Echo ' <br/> the member ID to remove is '. $cur->id;        }else{Echo ' <br/> the member to remove is not ';}}/** * traverse all nodes and output display * * @access public * @param obj Head initial node */public static function Sho Wrank ($head) {$cur = $head->next;//Do not print empty node while ($cur->next! = null) {echo ' <br/>id= '  . $cur->id. ' '.'            Username= '. $cur->username;        $cur = $cur->next;  } Echo ' <br/>id= '. $cur->id. ' '.'      Username= '. $cur->username; }}//Create an initial node $head=new rank ();//Create a member of $rank=new rank (1, ' Lao Wang '); Rank::addrank ($head, $rank); $rank =new rank (2, ' xiaoming '); Rank::addrank ($head, $rank); $rank =new rank (6, ' Big Bear '); Rank::addrank ($head, $rank); $rank =new rank (3, ' static fragrance '); Rank::addrank ($head, $rank); $rank =new rank (56, ' Erniang '); Rank::addrank ($head, $rank); Echo ' <br/> member leaderboard ...; Rank::showrank ($head); Echo ' <br/> '; Echo ' <br/> deleted member leaderboard ... '; Rank::d Elrank ($head, 3); Rank::showrank ($head); Echo ' <br/> '; Echo ' <br/> the following test deletes the first and last member <br/> '; Echo ' <br/> The member leaderboard after deletion ... '; Rank::d Elrank ($head, 1); Rank::showrank ($head); ECho ' <br/> '; Echo ' <br/> deleted member leaderboard ... '; Rank::d Elrank ($head, 56); Rank::showrank ($head);? >

The

follows a bidirectional list of internal implementations of PHP, which is interesting to follow.

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.