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. Last time I shared "PHP Mini Tutorial: Implementing Linked Lists", this time I would like to add a few words about doubly linked lists.
Copy code The code is as follows:
class Hero
{
public $pre=null;
public $no;
public $name;
public $next=null ;
public function __construct($no='',$name='')
{
$this->no=$no;
name=$name;
}
static public function addHero($head,$hero)
{
$cur = $head;
$isExist=false;
//Determine whether the current linked list is empty $hero->pre=$cur;
while($cur->next!=null)
{
Break; ->no == $hero->no)
lt;br>The same number cannot be added";
=$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;
}
}
}
//遍历
static public function showHero($head)
{
$cur=$head;
while($cur->next!=null)
{
echo "
编号:".$cur->next->no."名字:".$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;
}
)
$cur->pre->next=$cur->next;
}
hero1 = new Hero(1,'1111');
$hero3 = new Hero(3,'3333');
$hero2 = new Hero(2,'2222');
Hero:: addHero($head,$hero1);
Hero::addHero($head,$hero3);
Hero::addHero($head,$hero2);
Hero::showHero($head) ;
Hero::delHero($head,2);
Hero::showHero($head);
?>
http://www.bkjia.com/PHPjc/788644.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/788644.html
TechArticle
I 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 them. , share it with everyone. Last time I shared "The Facts of PHP Small Tutorial...