PHP singly linked list implementation_PHP tutorial

WBOY
Release: 2016-07-20 11:15:51
Original
1192 people have browsed it

PHP singly linked list implementation

<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>?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440162.htmlTechArticlephp singly linked list implementation = __construct( ='', ='' -no= -name= addHero( , = = ( - != ( - -no - = - - = - = = ; ( - -no== -no){ "This position is occupied and no one is allowed to occupy it"; = ; { = - ( ){ - = sh...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!