PHP simple implementation of cyclic linked list function example, php loop example

Source: Internet
Author: User

PHP simple implementation of cyclic linked list function example, php loop example

This example describes PHP's simple implementation of the Circular linked list function. We will share this with you for your reference. The details are as follows:

Overview:

Cyclic linked list is another form of chained storage structure. It points the pointer field of the last node in the table to the header node, and the entire linked list forms a ring.

As shown in:

Implementation Code:

<?phpclass node{  public $data;  public $link;  public function __construct($data=null,$link=null){    $this->data=$data;    $this->link=$link;  }}class cycleLinkList{  public $head;  public function __construct($data,$link=null){    $this->head=new node($data,$link);    $this->head->link=$this->head;  }  public function insertLink($data){    $p=new node($data);    $q=$this->head->link;    $r=$this->head;    if($q==$r)    {      $q->link=$p;      $p->link=$q;      return;    }    while($q!=$this->head){      $r=$q;$q=$q->link;    }    $r->link=$p;    $p->link=$this->head;  }}$linklist=new cycleLinkList(1);for($i=2;$i<11;$i++){   $linklist->insertLink($i);}$q=$linklist->head->link;echo $linklist->head->data;while($q!=$linklist->head){  echo $q->data;  $q=$q->link;}echo "<br>--------------------------<br>";$p=$linklist->head;$r=$p;$n=10;$i=2;while($n){    while(0!=$i){    $r=$p;$p=$p->link;    $i--;    }    echo $p->data;    $r->link=$p->link;    $tmp=$p;    $p=$p->link;    unset($tmp);    $n--;    $i=2;}?>

Running result:

12345678910--------------------------36927185104

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.