php单链表实现_PHP教程

WBOY
풀어 주다: 2016-07-20 11:15:51
원래의
1199명이 탐색했습니다.

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>?>
로그인 후 복사

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440162.htmlTechArticlephp单链表实现 = __construct( ='', ='' -no= -name= addHero( , = = ( - != ( - -no - = - - = - = = ; ( - -no== -no){ "该位置已有人,不允许占位"; = ; { = - ( ){ - = sh...
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿