Maison > php教程 > php手册 > le corps du texte

PHP实现链表

WBOY
Libérer: 2016-06-13 09:34:06
original
1181 Les gens l'ont consulté

看了很久数据结构但是没有怎么用过,在网上看到了关于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>
Copier après la connexion
<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>);
Copier après la connexion

我是天王盖地虎的分割线                                                                

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

 

 

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal