Home > Backend Development > PHP Tutorial > PHP implementation of linked list_PHP tutorial

PHP implementation of linked list_PHP tutorial

WBOY
Release: 2016-07-13 10:28:42
Original
889 people have browsed it

I have been looking at data structures for a long time but have never used them. I saw the data structure of PHP on the Internet, studied it and shared it with everyone.

Short and undivided

<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>
Copy after login
<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>);
Copy after login

I am the dividing line between the king of heaven and the tiger on earth. Orderly insertion requires traversing the linked list, so I won’t introduce some knowledge about linked lists. Here I mainly share the code.

Please indicate the source for reprinting: http://www.cnblogs.com/yydcdut

http://www.bkjia.com/PHPjc/781162.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781162.htmlTechArticleI have been looking at data structures for a long time but have never used them. I saw about PHP data structures on the Internet and studied them. , share it with everyone. Short and uncut class Hero{ public $no...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template