首页 > php教程 > php手册 > 正文

php单链表实现

WBOY
发布: 2016-06-13 11:31:35
原创
1614 人浏览过

php单链表实现

<span <?<span php
</span><span //</span><span 单链表</span>
<span class</span><span  Hero{
</span><span public</span> <span $no</span><span ;
</span><span public</span> <span $name</span><span ; 
</span><span public</span> <span $nickname</span><span ;
</span><span public</span> <span $next</span>=<span null</span><span ;

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

}
</span><span function</span> addHero(<span $head</span>,<span $Hero</span><span ){
    </span><span $cur</span>=<span $head</span><span ;
    </span><span $flag</span>=<span true</span><span ;
    </span><span while</span> (<span $cur</span>-><span next</span>!=<span null</span><span ) {
        </span><span if</span>(<span $cur</span>-><span next</span>->no><span $Hero</span>-><span no){
            </span><span $tmp</span>=<span $cur</span>-><span next</span><span ;
            </span><span $cur</span>-><span next</span>=<span $Hero</span><span ;
            </span><span $Hero</span>-><span next</span>=<span $tmp</span><span ;
            </span><span $flag</span>=<span false</span>;<span break</span><span ;
        }</span><span else</span> <span if</span>(<span $cur</span>-><span next</span>->no==<span $Hero</span>->no){<span echo</span> "该位置已有人,不允许占位";<span $flag</span>=<span false</span>;<span break</span><span ;}
        </span><span else</span>{ <span $cur</span>=<span $cur</span>-><span next</span><span ;}
    }
     </span><span if</span>(<span $flag</span>){<span $cur</span>-><span next</span>=<span $Hero</span><span ;}
}
</span><span //</span><span 增加</span>
<span function</span> showHero(<span $head</span><span ){
    </span><span $cur</span>=<span $head</span><span ;
    </span><span while</span>(<span $cur</span>-><span next</span>!=<span null</span><span ){
        
        </span><span echo</span> <span $cur</span>-><span next</span>->no.":".<span $cur</span>-><span next</span>->name."<br/>"<span ;
        </span><span $cur</span>=<span $cur</span>-><span next</span><span ;
     }

 }
</span><span //</span><span 删除特定编号的</span>
 <span function</span> delHero(<span $head</span>,<span $no</span><span ){
     </span><span $cur</span>=<span $head</span><span ;
     </span><span while</span>(<span $cur</span>-><span next</span>!=<span null</span><span ){
         </span><span if</span>(<span $cur</span>-><span next</span>->no==<span $no</span><span )
             {</span><span if</span>(<span $cur</span>-><span next</span>-><span next</span>)<span $cur</span>-><span next</span>=<span $cur</span>-><span next</span>-><span next</span><span ;
                 </span><span else</span> <span $cur</span>-><span next</span>=<span null</span><span ;
                 </span><span break</span><span ;
             }
             </span><span $cur</span>=<span $cur</span>-><span next</span><span ;
     }

 }
 </span><span //</span><span 查找特定编号的信息</span>
 <span function</span> findHero(<span $head</span>,<span $no</span><span ){
     </span><span $cur</span>=<span $head</span><span ;
     </span><span while</span>(<span $cur</span>-><span next</span>!=<span null</span><span ){
         </span><span if</span>(<span $cur</span>-><span next</span>->no==<span $no</span>){<span break</span><span ;}
         </span><span $cur</span>=<span $cur</span>-><span next</span><span ;
     }
     </span><span echo</span><span $cur</span>-><span next</span>-><span name;

 }
 </span><span //</span><span 改特定编号的信息</span>
 <span function</span> updateHero(<span $head</span>,<span $no</span>,<span $name</span><span ){
     </span><span $cur</span>=<span $head</span><span ;
     </span><span while</span>(<span $cur</span>-><span next</span>!=<span null</span><span ){
         </span><span if</span>(<span $cur</span>-><span next</span>->no==<span $no</span>){<span break</span><span ;}
         </span><span $cur</span>=<span $cur</span>-><span next</span><span ;
     }
</span><span $cur</span>-><span next</span>->name=<span $name</span><span ;
 }
</span><span $head</span>=<span new</span><span  Hero();
</span><span $Hero</span>=<span new</span> Hero(1,"宋江"<span );
addHero(</span><span $head</span>,<span $Hero</span><span );
</span><span $Hero</span>=<span new</span> Hero(6,"林冲"<span );
addHero(</span><span $head</span>,<span $Hero</span><span );
</span><span $Hero</span>=<span new</span> Hero(2,"吴用"<span );
addHero(</span><span $head</span>,<span $Hero</span><span );
</span><span $Hero</span>=<span new</span> Hero(4,"李逵"<span );
addHero(</span><span $head</span>,<span $Hero</span><span );
showHero(</span><span $head</span><span );
</span><span //</span><span 删除4号</span>
delHero(<span $Hero</span>,4<span );
</span><span //</span><span 查找6号</span>
findHero(<span $head</span>,6<span );
</span><span //</span><span  修改6号</span>
updateHero(<span $Hero</span>,6,"林哥哥"<span );
showHero(</span><span $head</span><span );


</span></span>?>
登录后复制

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!