首页 > php教程 > php手册 > 正文

PHP实现链表

WBOY
发布: 2016-06-13 09:34:06
原创
1181 人浏览过

看了很久数据结构但是没有怎么用过,在网上看到了关于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>
登录后复制
<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>);
登录后复制

我是天王盖地虎的分割线                                                                

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

 

 

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板