php单链表实现_PHP教程

WBOY
Libérer: 2016-07-20 11:15:51
original
1200 Les gens l'ont consulté

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>?>
Copier après la connexion

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440162.htmlTechArticlephp单链表实现 = __construct( ='', ='' -no= -name= addHero( , = = ( - != ( - -no - = - - = - = = ; ( - -no== -no){ "该位置已有人,不允许占位"; = ; { = - ( ){ - = sh...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal