//定义英雄类
class Hero{
public $no;//排名
public $name;//real name
public $nickname;//nickname
Public $next=null;//$next is a reference pointing to another Hero object instance.
public function __construct($no='',$name='',$nickname=''){
$this->no=$no;
$this->name=$name;
$this->nickname=$nickname;
}
}
$head=new Hero();
//Write a function specifically for adding heroes
function addHero($head,$hero){
to
// Find the linked list at the end, never move $ head;
$cur=$head;
// 2. Add to the hero's ranking (here I hope to ensure the order of the linked list)
// Thinking: Be sure to talk first, and then write the code
$flag=false; //Indicates there are no duplicate numbers
while($cur->next!=null){
>
break;
}else if($cur->next->no==$hero->no){
$ Flag = TRUE; // If you enter here, it means that there are repeated, placed to true
echo'
Cannot grab the seat, '.$hero->no.'The seat is already occupied';
$cur=$cur->next;
}
to out ’’s ’ ’ s ’ out out out out out out out out out outmb out out together out together up to to 3 after- s t-luse to spouse to swipe at .
//Join
IF ($ FLAG == False) {// Only when $ flag is false, it means that the insertion will be executed without repeating. If the $flag flag is not added, although the above else if judgment is made and duplicates are encountered, they will still be inserted.
$hero->next=$cur->next;
$cur->next=$hero;
}
}
//是从head开始遍历的,$head头的值千万不能变,变化后就不能遍历我们的单链表了
function showHeros($head){
//遍历[必须要知道什么时候,到了链表的最后]
//这里为了不去改变$head的指向,我们可以使用一个临时的变量
$cur=$head;
while($cur->next!=null){
//第一个节点为头结点,所以用$cur->next指向下一个节点,头结点里什么都没有是空的
echo'
英雄的编号是'.$cur->next->no.'名字'.$cur->next->name.'外号='.$cur->next->nickname;
//如果只写到这里,就会是死循环了
//所有要让$cul移动
$cur=$cur->next;
}
}
//从链表中删除某个英雄
function delHero($head,$herono){
//首先要找到这个英雄在哪里
$cur=$head; //让$cur指向$head;
$flag=false; //Flag bit, assuming not found
while($cur->next!=null){
if($cur->next->no==$herono){
// Find the next node of the $ Cur is the node that should be deleted.
break;
$cur=$cur->next;
}
$cur->next=$cur->next->next;
}else{
echo'
There is no number of the hero you want to delete'.$herono;
}
}
//Modify the hero
function updateHero($head,$hero){
// Let’s find this hero first
while($cur->next!=null){
IF ($ Cur-& GT; Next-& GT; No == $ Hero-& GT; NO) {
break;
$cur=$cur->next;
}
using using the flag ‐
// After exiting the WHILE cycle, if $ Cur-& GT; Next == NULL indicates that the cur is to the end of the team, and no
if($cur->next==null){
echo '& lt; br/& gt; you need to modify'. $ Hero-& GT; no.
}else{
// The number cannot be modified, and the name and nickname can only be modified
$cur->next->name=$hero->name;
$cur->next->nickname=$hero->nickname;
}
}
//Add
$hero=new Hero(1,'Song Jiang','Timely Rain');
addHero($head,$hero);
$hero=new Hero(2,'Lu Junyi','Yu Qilin');
addHero($head,$hero);
$hero=new Hero(6,'Lin Chong','Leopard Head');
addHero($head,$hero);
$hero=new Hero(3,'Wu Yong','Zhi Duoxing');
addHero($head,$hero);
$hero=new Hero(1,'Song Jiang','Timely Rain');
addHero($head,$hero);
$hero=new Hero(2,'Lu Junyi','Yu Qilin');
addHero($head,$hero);
// When the output is output, Lin Chong is in front of Wu Yong, and Lin Chong is No. 6, which should be ranked behind Wu Yong. This is the disadvantage of the last adding to the linked list.
echo'
showHeros($head);
with with with with echo'
//delHero($head,1);
delHero($head,21); //Delete a non-existent
showHeros($head);
echo'
$hero=new Hero(1,'Hu Hansan','Youbaihu');
updateHero($head,$hero);
showHeros($head);
?>
The key is application, such as using a circular linked list to solve the problem of losing a handkerchief.
Why there is no idea, there is no algorithm and data structure, and there is no way to start.
If you knock down this singly linked list case yourself, you will find that it is not that difficult.
Using a two-way linked list with header to implement - Water Margin hero ranking management
Disadvantage analysis of one-way linked list: it cannot delete itself and needs to rely on auxiliary nodes.
Doubly linked lists can delete themselves. At the same time, in binary trees and generalized tables, one node needs to be used to perform the application of two or more nodes!
http://www.bkjia.com/PHPjc/477873.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477873.htmlTechArticlesingleLink.php [php] html head meta http-equiv=Content-Type content=text/html; cha