PHP small tutorial to implement linked list_PHP tutorial

WBOY
Release: 2016-07-13 10:28:46
Original
683 people have browsed it

I’ve been reading about data structures for a long time but haven’t used them much. I saw about PHP’s data structures on the Internet, studied them, and wanted to share them with you.

Copy code The code is as follows:

class Hero
{
public $no;//ranking
public $name;//name
public $next=null;//$next is a reference, pointing to Another Hero object instance

public function __construct($no='',$name='')
{
$this->no=$no;
$this ->name=$name;
}

static public function showList($head)
{
$cur = $head;
while($cur->next ! = NULL) {
Echo "Ranking:". $ Cur-& GT; NEXT- & GT; no. ", Name:". $ Cur- & GT; NEXT- & GT; "& LT; br & gt; ";
                                                                                 ​>        $cur = $head;
                                                                                                                                                                                                                    ;next=$hero;
}
//Insertion into ordered linked list
static public function addHeroSorted($head,$hero)
{
$cur = $head;
                                                                                                                                                                                                                                                                                   worse worse worse not be more? 🎜> }
                                                                                                            ’ ’ s ’     ’ ‐                    next =$tep;*/
                                                                                                                                                                          . deleteHero($head,$no)
{
$cur = $head;
while($cur->next->no != $no && $cur->next!= null ;) $cur -& gt; Next = $ Cur-& GT; Next-& GT; Next;
Echo "Delete the success. br>";
}
}

static public function updateHero($head,$hero)
{
$cur = $head;
while($cur- >next->no != $hero->no && $cur->next!= null)
                                                                                                   if($cur->next->no != null)
                                                                                                                                                           = $hero;
                          echo "Change successful
";                          
                                                   else
//Create head
$head = new Hero();
//The first one
$hero = new Hero(1,'111');
//Connection$head->next = $hero;

//Second
$hero2 = new Hero(3,'333');
//Connection
Hero::addHero( $head,$hero2);
$hero3 = new Hero(2,'222');
Hero::addHeroSorted($head,$hero3);
//Display
Hero:: showlist($head);
//Delete
Hero::deleteHero($head,4);
//Show
Hero::showlist($head);
//Change
$hero4=new Hero(2,'xxx');
Hero::updateHero($head,$hero4);
//Show
Hero::showlist($head);



Orderly insertion requires traversing the linked list, so I won’t introduce some knowledge about linked lists. Here I mainly share the code.


http://www.bkjia.com/PHPjc/781030.html

www.bkjia.com

true

TechArticleI have been looking at data structures for a long time but have never used them. I saw about PHP data structures on the Internet and studied it. , share it with everyone. Copy the code The code is as follows: class He...
Related labels:
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