Introduction to the PHP circular link list

Source: Internet
Author: User
This article mainly introduces the PHP ring list implementation method, combined with specific examples of the definition of PHP ring linked list, create and traverse the operation skills and considerations, the need for friends can refer to the next

This paper describes the implementation of the PHP ring list. Share to everyone for your reference, as follows:

A ring list is a chain-like storage structure, similar to a single-linked list. The difference is that the tail node of the ring list points to the head node.

Thus forming a ring,

The ring list is a very flexible storage structure that solves many practical problems, magician licensing problems, and Joseph's problems.

Can be used to solve the ring list, the following is a complete example of a ring list, using PHP to implement (refer to Hanshunping Teacher's PHP algorithm tutorial)


/** * ring List Implementation * */class child{public $no;//serial number public $next;//Pointer to the next node public  function __construct ($no = ") {$this->no = $no; }}/** * Create a ring list * @param $first the head node of the null list * @param $num the number of nodes that the integer needs to add */function Create (& $first, $num) {$cur  = NULL;    For ($i =0, $i < $num; $i + +) {$child = new Child ($i + 1);      if ($i ==0) {$first = $child; $first->next = $first;//link the tail node of the list to the head node to form a ring list $cur = $first;//The head node of the list cannot be moved to a temporary variable} else {$cur->next =      $child;    $cur->next->next = $first;//link the tail node of the list to the head node to form a ring-linked list $cur = $cur->next;  }}}/** * Loop linked list * @param $first the head of an object ring list * */function Show ($first) {//head node cannot move, make a temporary variable $cur = $first;    while ($cur->next!= $first)//when $cur->next== $first explained to the last node of the list {echo $cur->no. ' </br> ';  $cur = $cur->next; }//When exiting the loop, $cur->next= $first just ignore the traversal of the current node itself, so when you exit, you will have to output a little bit of the node echo $cur->no;} 

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.