php中文乱码 php实现单链表

WBOY
Freigeben: 2016-07-28 08:28:08
Original
928 Leute haben es durchsucht

<span>php
</span><span>/*</span><span>*
  * 单链表
  </span><span>*/</span><span>class</span><span> Demo
{
    </span><span>private</span><span>$id</span><span>;
    </span><span>public</span><span>$name</span><span>;
    </span><span>public</span><span>$next</span><span>;

    </span><span>public</span><span>function</span> __construct (<span>$id</span> = '', <span>$name</span> = ''<span>)
    {
        </span><span>$this</span>->id = <span>$id</span><span>;
        </span><span>$this</span>->name = <span>$name</span><span>;
    }

    </span><span>static</span><span>public</span><span>function</span> show (<span>$head</span><span>)
    {
        </span><span>$cur</span> = <span>$head</span><span>;
        </span><span>while</span> (<span>$cur</span>-><span>next</span><span>) {
            </span><span>echo</span><span>$cur</span>-><span>next</span>->id,'###',<span>$cur</span>-><span>next</span>->name,'<br>'<span>;
            </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>echo</span> '<hr>'<span>;
    }

    </span><span>//</span><span>尾插法</span><span>static</span><span>public</span><span>function</span> push (<span>$head</span>, <span>$node</span><span>)
    {
        </span><span>$cur</span> = <span>$head</span><span>;
        </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) {
            </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>$cur</span>-><span>next</span>  = <span>$node</span><span>;
        </span><span>return</span><span>$head</span><span>;
    }

    </span><span>static</span><span>public</span><span>function</span> insert(<span>$head</span>, <span>$node</span><span>)
    {
        </span><span>$cur</span> = <span>$head</span><span>;
        </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) {
            </span><span>if</span> (<span>$cur</span>-><span>next</span>->id > <span>$node</span>-><span>id) {
                </span><span>break</span><span>;
            }
            </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>$node</span>-><span>next</span> = <span>$cur</span>-><span>next</span><span>;
        </span><span>$cur</span>-><span>next</span>  = <span>$node</span><span>;
        </span><span>return</span><span>$head</span><span>;
    }

    </span><span>static</span><span>public</span><span>function</span> edit(<span>$head</span>, <span>$node</span><span>)
    {
        </span><span>$cur</span> = <span>$head</span><span>;
        </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) {
            </span><span>if</span> (<span>$cur</span>-><span>next</span>->id == <span>$node</span>-><span>id) {
                </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>$node</span>-><span>name;
        </span><span>return</span><span>$head</span><span>;        
    }

    </span><span>static</span><span>public</span><span>function</span> pop (<span>$head</span>, <span>$node</span><span>)
    {
        </span><span>$cur</span> = <span>$head</span><span>;
        </span><span>while</span> (<span>NULL</span> != <span>$cur</span>-><span>next</span><span>) {
            </span><span>if</span> (<span>$cur</span>-><span>next</span> == <span>$node</span><span>) {
                </span><span>break</span><span>;
            }
            </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>$cur</span>-><span>next</span> = <span>$node</span>-><span>next</span><span>;
        </span><span>return</span><span>$head</span><span>;            
    }
}

</span><span>$team</span> = <span>new</span><span> Demo();
</span><span>$node1</span> = <span>new</span> Demo(1, '唐三藏'<span>);
Demo</span>::push(<span>$team</span>, <span>$node1</span><span>);
</span><span>$node1</span>->name = '唐僧'<span>;
Demo</span>::show(<span>$team</span><span>);

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

Demo</span>::show(<span>$team</span>);
Nach dem Login kopieren

以上就介绍了php中文乱码 php实现单链表,包括了php中文乱码方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!