php单链表实现_PHP教程

WBOY
Freigeben: 2016-07-20 11:15:51
Original
1201 Leute haben es durchsucht

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>?>
Nach dem Login kopieren

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440162.htmlTechArticlephp单链表实现 = __construct( ='', ='' -no= -name= addHero( , = = ( - != ( - -no - = - - = - = = ; ( - -no== -no){ "该位置已有人,不允许占位"; = ; { = - ( ){ - = sh...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage