PHP中模拟链表和链表的基本操作示例
模拟链表:
<?php /** *PHP实现链表的基本操作 */ classlinkList{ /** *姓名 *@varstring */ public$name=''; /** *编号 *@varint */ public$id=0; /* *引用下一个对象 */ public$next=null; /** *构造函数初始化数据 *@paramint$id *@paramstring$name */ publicfunction__construct($id=0,$name=''){ $this->name=$name; $this->id=$id; } /** *遍历链表 */ publicstaticfunctionecho_link_list($head){ $curr=$head; while($curr->next!=null){ echo'姓名:'.$curr->next->name,'编号:'.$curr->next->id; echo'<br>'; $curr=$curr->next; } } /** *添加新节点 */ publicstaticfunctionadd($head,$id,$name){ $curr=$head; $obj=newlinkList($id,$name); while($curr->next!=null){ //如果当前ID<下一个ID,则添加到中间,添加节点到指定顺序位置 if($curr->next->id>$id){ $obj->next=$curr->next; $curr->next=$obj; returntrue; }elseif($curr->next->id==$id){ echo'当前Id:'.$id.'重复了,请不要继续添加了!'; echo'<br>'; returnfalse; } $curr=$curr->next; } //添加节点到尾部 if($curr->next==null){ $curr->next=$obj; } } /** *删除节点 */ publicstaticfunctiondel($head,$id){ $curr=$head; while($curr->next!=null){ if($curr->next->id==$id){ $curr->next=$curr->next->next; returntrue; } $curr=$curr->next; } } /** *修改节点 */ publicstaticfunctionedit($head,$id,$new_name){ $curr=$head; while($curr->next!=null){ if($curr->next->id==$id){ $curr->next->name=$new_name; } $curr=$curr->next; } } } $head=newlinkList(); linkList::add($head,1,'wangdk'); linkList::add($head,2,'sunshuzhen'); linkList::add($head,8,'wanghaha'); linkList::add($head,6,'wangchufen'); linkList::add($head,6,'wangchufen'); linkList::add($head,3,'wangdaye'); linkList::del($head,1); linkList::edit($head,2,'hahaha'); linkList::echo_link_list($head); ?>
链表的增删查改:
<?php /** *PHP实现链表的基本操作 */ classlinkList{ /** *姓名 *@varstring */ public$name=''; /** *编号 *@varint */ public$id=0; /* *引用下一个对象 */ public$next=null; /** *构造函数初始化数据 *@paramint$id *@paramstring$name */ publicfunction__construct($id=0,$name=''){ $this->name=$name; $this->id=$id; } /** *遍历链表 */ publicstaticfunctionecho_link_list($head){ $curr=$head; while($curr->next!=null){ echo'姓名:'.$curr->next->name,'编号:'.$curr->next->id; echo'<br>'; $curr=$curr->next; } } /** *添加新节点 */ publicstaticfunctionadd($head,$id,$name){ $curr=$head; $obj=newlinkList($id,$name); while($curr->next!=null){ //如果当前ID<下一个ID,则添加到中间,添加节点到指定顺序位置 if($curr->next->id>$id){ $obj->next=$curr->next; $curr->next=$obj; returntrue; }elseif($curr->next->id==$id){ echo'当前Id:'.$id.'重复了,请不要继续添加了!'; echo'<br>'; returnfalse; } $curr=$curr->next; } //添加节点到尾部 if($curr->next==null){ $curr->next=$obj; } } /** *删除节点 */ publicstaticfunctiondel($head,$id){ $curr=$head; while($curr->next!=null){ if($curr->next->id==$id){ $curr->next=$curr->next->next; returntrue; } $curr=$curr->next; } } /** *修改节点 */ publicstaticfunctionedit($head,$id,$new_name){ $curr=$head; while($curr->next!=null){ if($curr->next->id==$id){ $curr->next->name=$new_name; } $curr=$curr->next; } } } $head=newlinkList(); linkList::add($head,1,'wangdk'); linkList::add($head,2,'sunshuzhen'); linkList::add($head,8,'wanghaha'); linkList::add($head,6,'wangchufen'); linkList::add($head,6,'wangchufen'); linkList::add($head,3,'wangdaye'); linkList::del($head,1); linkList::edit($head,2,'hahaha'); linkList::echo_link_list($head); ?>