PHP小教程之实现链表


Posted in PHP onJune 09, 2014

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

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

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

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

PHP 相关文章推荐
php cookie 登录验证示例代码
Mar 16 PHP
PHP分页效率终结版(推荐)
Jul 01 PHP
基于preg_match_all采集后数据处理的一点心得笔记(编码转换和正则匹配)
Jan 31 PHP
ThinkPHP3.1数据CURD操作快速入门
Jun 19 PHP
php限制ip地址范围的方法
Mar 31 PHP
php采集中国代理服务器网的方法
Jun 16 PHP
屏蔽PHP默认设置中的Notice警告的方法
May 20 PHP
PHP在线打包下载功能示例
Oct 15 PHP
PHP文件管理之实现网盘及压缩包的功能操作
Sep 20 PHP
基于 Swoole 的微信扫码登录功能实现代码
Jan 15 PHP
Laravel5.0+框架邮件发送功能实现方法图文与实例详解
Apr 23 PHP
WordPress伪静态规则设置代码实例
Dec 10 PHP
浅谈Eclipse PDT调试PHP程序
Jun 09 #PHP
教你如何在CI框架中使用 .htaccess 隐藏url中index.php
Jun 09 #PHP
PHP、Nginx、Apache中禁止网页被iframe引用的方法
Oct 01 #PHP
PHP遍历目录并返回统计目录大小
Jun 09 #PHP
php中替换字符串中的空格为逗号','的方法
Jun 09 #PHP
使用php批量删除数据库下所有前缀为prefix_的表
Jun 09 #PHP
PHP OPP机制和模式简介(抽象类、接口和契约式编程)
Jun 09 #PHP
You might like
PHP 实现explort() 功能的详解
2013/06/20 PHP
PHP数组排序函数合集 以及它们之间的联系分析
2013/06/27 PHP
php连接odbc数据源并保存与查询数据的方法
2014/12/24 PHP
PHP 绘制网站登录首页图片验证码
2016/04/12 PHP
PHP7 标准库修改
2021/03/09 PHP
JS中的this变量的使用介绍
2013/10/21 Javascript
javascript中文本框中输入法切换的问题
2013/12/10 Javascript
Extjs4中的分页应用结合前后台
2013/12/13 Javascript
JavaScript的null和undefined区别示例介绍
2014/09/15 Javascript
Javascript基础教程之argument 详解
2015/01/18 Javascript
jQuery操作基本控件方法实例分析
2015/12/31 Javascript
IE下JS保存图片的简单实例
2016/07/15 Javascript
AngularJS基础 ng-model-options 指令简单示例
2016/08/02 Javascript
jQuery对table表格进行增删改查
2020/12/22 Javascript
angularJs-$http实现百度搜索时的动态下拉框示例
2018/02/27 Javascript
layui实现下拉框三级联动
2019/07/26 Javascript
js实现简易点击切换显示或隐藏
2020/11/29 Javascript
使用Python 正则匹配两个特定字符之间的字符方法
2018/12/24 Python
Pandas分组与排序的实现
2019/07/23 Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
2019/08/07 Python
Python线程障碍对象Barrier原理详解
2019/12/02 Python
浅谈pytorch池化maxpool2D注意事项
2020/02/18 Python
Python 线性回归分析以及评价指标详解
2020/04/02 Python
python thrift 实现 单端口多服务的过程
2020/06/08 Python
css3实现超立体3D图片侧翻倾斜效果
2014/04/16 HTML / CSS
HTML5实现移动端复制功能
2018/04/19 HTML / CSS
英国护发和美妆在线商店:Klip Shop
2019/03/24 全球购物
会计毕业生自我鉴定
2013/11/04 职场文书
运动会100米解说词
2014/01/23 职场文书
淘宝好评语大全
2014/05/05 职场文书
关于销售人员的年终工作总结要点
2019/08/15 职场文书
话题作文之成长
2019/12/09 职场文书
Python WSGI 规范简介
2021/04/11 Python
HAM-2000摩机图
2021/04/22 无线电
Python爬取用户观影数据并分析用户与电影之间的隐藏信息!
2021/06/29 Python
【DOTA2】总决赛血虐~ XTREME GAMING vs MAGMA - OGA DOTA PIT 2022 CN
2022/04/02 DOTA