Rumah >pembangunan bahagian belakang >tutorial php >PHP实现链表_PHP教程

PHP实现链表_PHP教程

WBOY
WBOYasal
2016-07-13 10:28:42890semak imbas

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/781162.htmlTechArticle看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下。 简短不割 class Hero{ public $no...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn