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 相关文章推荐
一个颜色轮换的简单例子
Oct 09 PHP
php跨域cookie共享使用方法
Feb 20 PHP
php检查字符串中是否包含7位GSM字符的方法
Mar 17 PHP
PHP模拟QQ登录的方法
Jul 29 PHP
PHP实现文件上传和多文件上传
Dec 24 PHP
php面向对象值单例模式
May 03 PHP
详解如何在云服务器上部署Laravel
Jun 30 PHP
阿里云PHP SMS短信服务验证码发送方法
Jul 11 PHP
ThinkPHP框架整合微信支付之Native 扫码支付模式一图文详解
Apr 09 PHP
PHP延迟静态绑定使用方法实例解析
Sep 05 PHP
详解PHP用mb_string处理windows中文字符
May 26 PHP
Linux系统下安装PHP7.3版本
Jun 26 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
2006/12/23 PHP
Linux下PHP加速器APC的安装与配置笔记
2014/10/24 PHP
php查找字符串中第一个非0的位置截取
2017/02/27 PHP
PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例
2019/09/09 PHP
PHP反射原理与用法深入分析
2019/09/28 PHP
加速IE的Javascript document输出的方法
2010/12/02 Javascript
Javascript生成json的函数代码(可以用php的json_decode解码)
2012/06/11 Javascript
一个Action如何调用两个不同的方法
2014/05/22 Javascript
js实现网页自动刷新可制作节日倒计时效果
2014/05/27 Javascript
jQuery实现高亮显示的方法
2015/03/10 Javascript
详解使用grunt完成requirejs的合并压缩和js文件的版本控制
2017/03/02 Javascript
Vue项目中ESlint规范示例代码
2019/07/04 Javascript
vue-cli3+typescript新建一个项目的思路分析
2019/08/06 Javascript
python在指定目录下查找gif文件的方法
2015/05/04 Python
Python对文件操作知识汇总
2016/05/15 Python
用 Python 连接 MySQL 的几种方式详解
2018/04/04 Python
Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】
2018/12/24 Python
python二维键值数组生成转json的例子
2019/12/06 Python
Python列表如何更新值
2020/05/27 Python
CSS3动画之流彩文字效果+图片模糊效果+边框伸展效果实现代码合集
2017/08/18 HTML / CSS
在css3中background-clip属性与background-origin属性的用法介绍
2012/11/13 HTML / CSS
CSS3实现多样的边框效果
2018/05/04 HTML / CSS
欧舒丹英国官网:购买欧舒丹护手霜等明星产品
2017/01/17 全球购物
俄罗斯最大的隐形眼镜销售网站:Ochkov.Net
2021/02/07 全球购物
职专应届生求职信
2013/11/16 职场文书
电大毕业自我鉴定
2014/02/03 职场文书
劲霸男装广告词
2014/03/21 职场文书
不尊敬老师的检讨书
2014/12/21 职场文书
关于拾金不昧的感谢信
2015/01/21 职场文书
2015年七夕情人节活动方案
2015/05/06 职场文书
户外亲子活动总结
2015/05/08 职场文书
入党介绍人考察意见
2015/06/01 职场文书
Go语言 go程释放操作(退出/销毁)
2021/04/30 Golang
基于tensorflow权重文件的解读
2021/05/26 Python
postman中form-data、x-www-form-urlencoded、raw、binary的区别介绍
2022/01/18 HTML / CSS
Redis中有序集合的内部实现方式的详细介绍
2022/03/16 Redis