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;}