Example of simple implementation of circular linked list function in PHP

*文
Release: 2023-03-18 08:38:02
Original
1478 people have browsed it

This article mainly introduces the simple implementation of the circular linked list function in PHP, briefly describes the concept and functions of the circular linked list, and analyzes the PHP definition and related operating skills of using the circular linked list in the form of examples. Friends who need it can refer to it

Overview:

Circular linked list is another form of linked storage structure. Its characteristic is that the pointer field of the last node in the list points to the head node, and the entire linked list forms a ring.

As shown below:

Example of simple implementation of circular linked list function in PHP


##Implementation code:

<?php
class 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;
}
Copy after login


Run result:

12345678910
--------------------------
36927185104
Copy after login


##Related reading:

How to generate blurry images in PHP

Detailed explanation of PHP socket push technology

How to achieve case conversion in PHP?

The above is the detailed content of Example of simple implementation of circular linked list function in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!