Heim > php教程 > php手册 > Hauptteil

PHP实现链表

WBOY
Freigeben: 2016-06-13 09:34:06
Original
1181 Leute haben es durchsucht

看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下。

简短不割                                                                                     

<span>class</span><span> Hero
{
    </span><span>public</span> <span>$no</span>;<span>//</span><span>排名</span>
    <span>public</span> <span>$name</span>;<span>//</span><span>名字</span>
    <span>public</span> <span>$next</span>=<span>null</span>;<span>//</span><span>$next是一个引用,指向另外一个Hero的对象实例</span>
    
    <span>public</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>static</span> <span>public</span> <span>function</span> showList(<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>
Nach dem Login kopieren
<span>    <span>//普通插入</span>
    </span><span>static</span> <span>public</span> <span>function</span> addHero(<span>$head</span>,<span>$hero</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>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>$cur</span>-><span>next</span>=<span>$hero</span><span>;
    }
    <span>//有序的链表的插入</span>  
    </span><span>static</span> <span>public</span> <span>function</span> addHeroSorted(<span>$head</span>,<span>$hero</span><span>)
    {
        </span><span>$cur</span> = <span>$head</span><span>;
        </span><span>$addNo</span> = <span>$hero</span>-><span>no;
        </span><span>while</span>(<span>$cur</span>-><span>next</span>->no <= <span>$addNo</span><span>)
        {
            </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>/*</span><span>$tep = new Hero();
        $tep = $cur->next;
        $cur->next = $hero;
        $hero->next =$tep;</span><span>*/</span>
        <span>$hero</span>-><span>next</span>=<span>$cur</span>-><span>next</span><span>;
        </span><span>$cur</span>-><span>next</span>=<span>$hero</span><span>;
    }
    
    </span><span>static</span> <span>public</span> <span>function</span> deleteHero(<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>->no != <span>$no</span> && <span>$cur</span>-><span>next</span>!= <span>null</span><span>)
        {
            </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>if</span>(<span>$cur</span>-><span>next</span>->no != <span>null</span><span>)
        {
            </span><span>$cur</span>-><span>next</span> = <span>$cur</span>-><span>next</span>-><span>next</span><span>;
            </span><span>echo</span> "删除成功<br>"<span>; 
        }
        </span><span>else</span><span>
        {
            </span><span>echo</span> "没有找到<br>"<span>; 
        }
    }
    
    </span><span>static</span> <span>public</span> <span>function</span> updateHero(<span>$head</span>,<span>$hero</span><span>)
    {
        </span><span>$cur</span> = <span>$head</span><span>;
        </span><span>while</span>(<span>$cur</span>-><span>next</span>->no != <span>$hero</span>->no && <span>$cur</span>-><span>next</span>!= <span>null</span><span>)
        {
            </span><span>$cur</span> = <span>$cur</span>-><span>next</span><span>;
        }
        </span><span>if</span>(<span>$cur</span>-><span>next</span>->no != <span>null</span><span>)
        {
            </span><span>$hero</span>-><span>next</span> = <span>$cur</span>-><span>next</span>-><span>next</span><span>;
            </span><span>$cur</span>-><span>next</span> = <span>$hero</span><span>;
            </span><span>echo</span> "更改成功<br>"<span>; 
        }
        </span><span>else</span><span>
        {
            </span><span>echo</span> "没有找到<br>"<span>; 
        }
    }
}

</span><span>//</span><span>创建head头</span>
<span>$head</span> = <span>new</span><span> Hero();
</span><span>//</span><span>第一个</span>
<span>$hero</span> = <span>new</span> Hero(1,'111'<span>);
</span><span>//</span><span>连接</span>
<span>$head</span>-><span>next</span> = <span>$hero</span><span>;
</span><span>//</span><span>第二个</span>
<span>$hero2</span> = <span>new</span> Hero(3,'333'<span>);
</span><span>//</span><span>连接</span>
Hero::addHero(<span>$head</span>,<span>$hero2</span><span>);
</span><span>$hero3</span> = <span>new</span> Hero(2,'222'<span>);
Hero</span>::addHeroSorted(<span>$head</span>,<span>$hero3</span><span>);
</span><span>//</span><span>显示</span>
Hero::showlist(<span>$head</span><span>);
</span><span>//</span><span>删除</span>
Hero::deleteHero(<span>$head</span>,4<span>);
</span><span>//</span><span>显示</span>
Hero::showlist(<span>$head</span><span>);
</span><span>//</span><span>更改</span>
<span>$hero4</span>=<span>new</span> Hero(2,'xxx'<span>);
Hero</span>::updateHero(<span>$head</span>,<span>$hero4</span><span>);
</span><span>//</span><span>显示</span>
Hero::showlist(<span>$head</span>);
Nach dem Login kopieren

我是天王盖地虎的分割线                                                                

有序的插入的话需要遍历一遍链表,链表的一些知识就不介绍了哈。这里主要分享一下代码。

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut

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 Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage