Home > php教程 > php手册 > PHP implements singly linked list

PHP implements singly linked list

WBOY
Release: 2016-07-06 13:28:16
Original
905 people have browsed it
<?<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>);
Copy after login

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template