PHP circular Linked List Implementation Method example, php circular implementation example

Source: Internet
Author: User

PHP circular Linked List Implementation Method example, php circular implementation example

This article describes how to implement a PHP circular linked list. We will share this with you for your reference. The details are as follows:

A circular linked list is a chained storage structure similar to a single-chain table. The difference is that the end node of the circular linked list points to the header node.

To form a ring,

The circular linked list is a flexible storage structure that can solve many practical problems, such as the magician licensing and Joseph problems.

All can be solved by using the ring linked list. Below is a complete ring linked list instance, which is implemented using php (refer to the php algorithm tutorial of Han shunping)

/*** Implementation of the ring linked list **/class child {public $ no; // serial number public $ next; // pointer to the next node public function _ construct ($ no = '') {$ this-> no = $ no ;}} /*** create a circular linked list * @ param $ first null head node of the linked list * @ param $ num integer number of nodes to be added */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; // point the End Node of the linked list to the head node to form a circular linked list $ cur = $ first; // The head node of the linked list cannot be moved You need to give a temporary variable} else {$ cur-> next = $ child; $ cur-> next = $ first; // point the End Node of the linked list to the header node to form a circular linked list $ cur = $ cur-> next ;}}} /*** traverse the ring linked list * @ param $ first object head of the ring linked list **/function show ($ first) {// the header node cannot be moved, submit a temporary variable $ cur = $ first; while ($ cur-> next! = $ First) // when $ cur-> next = $ first, it indicates the last node of the linked list {echo $ cur-> no. '</br>'; $ cur = $ cur-> next ;} // when you exit the loop, $ cur-> next = $ first will ignore the traversal of the current node. Therefore, you need to output the result when you exit the loop. Otherwise, the system will traverse one node with less 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.