PHP Small tutorial Implementation bidirectional linked list _php tutorial

Source: Internet
Author: User
Read a long time data structure but not how to use, in the Internet to see the data structure about PHP, learning a bit, and share with you. The last time I shared the "PHP small tutorial implementation linked list," This time to add a doubly linked list.

Copy CodeThe code is as follows:
Class Hero
{
Public $pre =null;
Public $no;
Public $name;
Public $next =null;
Public function __construct ($no = ", $name =")
{
$this->no= $no;
$this->name= $name;
}
static public Function Addhero ($head, $hero)
{
$cur = $head;
$isExist =false;
Determine if the current list is empty
if ($cur->next==null)
{
$cur->next= $hero;
$hero->pre= $cur;
}
Else
{
If it is not an empty node, schedule the name to add
Find the added location
while ($cur->next!=null)
{
if ($cur->next->no > $hero->no)
{
Break
}
else if ($cur->next->no = = $hero->no)
{
$isExist =true;
echo "
Cannot add the same number ";
}
$cur = $cur->next;
}
if (! $isExist)
{
if ($cur->next!=null)
{
$hero->next= $cur->next;
}
$hero->pre= $cur;
if ($cur->next!=null)
{
$hero->next->pre= $hero;
}
$cur->next= $hero;
}
}
}
Traverse
static public Function Showhero ($head)
{
$cur = $head;
while ($cur->next!=null)
{
echo "
Number: ". $cur->next->no." Name: ". $cur->next->name;
$cur = $cur->next;
}
}
static public Function Delhero ($head, $herono)
{
$cur = $head;
$isFind =false;
while ($cur!=null)
{
if ($cur->no== $herono)
{
$isFind =true;
Break
}
Keep looking
$cur = $cur->next;
}
if ($isFind)
{
if ($cur->next!=null)
{
$cur->next_pre= $cur->pre;
}
$cur->pre->next= $cur->next;
}
Else
{
echo "
No target found ";
}
}
}
$head = new Hero ();
$hero 1 = new Hero (1, ' 1111 ');
$hero 3 = new Hero (3, ' 3333 ');
$hero 2 = new Hero (2, ' 2222 ');
Hero::addhero ($head, $hero 1);
Hero::addhero ($head, $hero 3);
Hero::addhero ($head, $hero 2);
Hero::showhero ($head);
Hero::d Elhero ($head, 2);
Hero::showhero ($head);
?>

http://www.bkjia.com/PHPjc/788644.html www.bkjia.com true http://www.bkjia.com/PHPjc/788644.html techarticle read a long time data structure but not how to use, in the Internet to see the data structure about PHP, learning a bit, and share with you. The last time I shared the little php tutorial ...

  • Related Article

    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.