首頁 > php教程 > php手册 > php实现单链表

php实现单链表

WBOY
發布: 2016-07-06 13:28:16
原創
894 人瀏覽過
<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
  * 单链表
  </span><span style="color: #008000;">*/</span> 
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Demo
{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$id</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$name</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$next</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct (<span style="color: #800080;">$id</span> = '', <span style="color: #800080;">$name</span> = ''<span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->id = <span style="color: #800080;">$id</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->name = <span style="color: #800080;">$name</span><span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> show (<span style="color: #800080;">$head</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$head</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> (<span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span>->id,'###',<span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span>->name,'<br>'<span style="color: #000000;">;
            </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">echo</span> '<hr>'<span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">尾插法</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> push (<span style="color: #800080;">$head</span>, <span style="color: #800080;">$node</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$head</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">NULL</span> != <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">) {
            </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span>  = <span style="color: #800080;">$node</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$head</span><span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> insert(<span style="color: #800080;">$head</span>, <span style="color: #800080;">$node</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$head</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">NULL</span> != <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span>->id > <span style="color: #800080;">$node</span>-><span style="color: #000000;">id) {
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            }
            </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$node</span>-><span style="color: #008080;">next</span> = <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span>  = <span style="color: #800080;">$node</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$head</span><span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> edit(<span style="color: #800080;">$head</span>, <span style="color: #800080;">$node</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$head</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">NULL</span> != <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span>->id == <span style="color: #800080;">$node</span>-><span style="color: #000000;">id) {
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            }
            </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span>->name = <span style="color: #800080;">$node</span>-><span style="color: #000000;">name;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$head</span><span style="color: #000000;">;        
    }

    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> pop (<span style="color: #800080;">$head</span>, <span style="color: #800080;">$node</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$head</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">NULL</span> != <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span> == <span style="color: #800080;">$node</span><span style="color: #000000;">) {
                </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
            }
            </span><span style="color: #800080;">$cur</span> = <span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span><span style="color: #000000;">;
        }
        </span><span style="color: #800080;">$cur</span>-><span style="color: #008080;">next</span> = <span style="color: #800080;">$node</span>-><span style="color: #008080;">next</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$head</span><span style="color: #000000;">;            
    }
}

</span><span style="color: #800080;">$team</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Demo();
</span><span style="color: #800080;">$node1</span> = <span style="color: #0000ff;">new</span> Demo(1, '唐三藏'<span style="color: #000000;">);
Demo</span>::push(<span style="color: #800080;">$team</span>, <span style="color: #800080;">$node1</span><span style="color: #000000;">);
</span><span style="color: #800080;">$node1</span>->name = '唐僧'<span style="color: #000000;">;
Demo</span>::show(<span style="color: #800080;">$team</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;"> Demo::show($team);</span>
<span style="color: #800080;">$node2</span> = <span style="color: #0000ff;">new</span> Demo(2, '孙悟空'<span style="color: #000000;">);
Demo</span>::insert(<span style="color: #800080;">$team</span>, <span style="color: #800080;">$node2</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Demo::show($team);</span>
<span style="color: #800080;">$node3</span> = <span style="color: #0000ff;">new</span> Demo(5, '白龙马'<span style="color: #000000;">);
Demo</span>::push(<span style="color: #800080;">$team</span>, <span style="color: #800080;">$node3</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Demo::show($team);</span>
<span style="color: #800080;">$node4</span> = <span style="color: #0000ff;">new</span> Demo(3, '猪八戒'<span style="color: #000000;">);
Demo</span>::insert(<span style="color: #800080;">$team</span>, <span style="color: #800080;">$node4</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Demo::show($team);</span>
<span style="color: #800080;">$node5</span> = <span style="color: #0000ff;">new</span> Demo(4, '沙和尚'<span style="color: #000000;">);
Demo</span>::insert(<span style="color: #800080;">$team</span>, <span style="color: #800080;">$node5</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Demo::show($team);</span>
<span style="color: #800080;">$node4</span>->name = '猪悟能';<span style="color: #008000;">//</span><span style="color: #008000;">php对象传引用,所以Demo::edit没有必要
// unset($node4);
// $node4 = new Demo(3, '猪悟能');
// Demo::edit($team, $node4);</span>
Demo::pop(<span style="color: #800080;">$team</span>, <span style="color: #800080;">$node1</span><span style="color: #000000;">);

Demo</span>::show(<span style="color: #800080;">$team</span>);
登入後複製

 

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板