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实现选择排序的解决方法
May 04 PHP
php增删改查示例自己写的demo
Sep 04 PHP
php获取qq用户昵称和在线状态(实例分析)
Oct 27 PHP
CodeIgniter生成网站sitemap地图的方法
Nov 13 PHP
php反射应用示例
Feb 25 PHP
PHP中的gzcompress、gzdeflate、gzencode函数详解
Jul 29 PHP
Yii框架表单模型和验证用法
May 20 PHP
php使用ffmpeg向视频中添加文字字幕的实现方法
May 23 PHP
PHP date()格式MySQL中插入datetime方法
Jan 29 PHP
laradock环境docker-compose操作详解
Jul 29 PHP
Thinkphp 框架扩展之数据库驱动常用方法小结
Apr 23 PHP
php去除数组中为0的元素的实例分析
Nov 17 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
第七节--类的静态成员
2006/11/16 PHP
php专用数组排序类ArraySortUtil用法实例
2015/04/03 PHP
php生成毫秒时间戳的实例讲解
2017/09/22 PHP
JavaScript的目的分析
2007/01/05 Javascript
js统计录入文本框中字符的个数并加以限制不超过多少
2014/05/23 Javascript
javascript 原型链维护和继承详解
2014/11/26 Javascript
js函数内变量的作用域分析
2015/01/12 Javascript
node.js+jQuery实现用户登录注册AJAX交互
2017/04/28 jQuery
JS实现求数组起始项到终止项之和的方法【基于数组扩展函数】
2017/06/13 Javascript
Angularjs实现下拉框联动的示例代码
2017/08/22 Javascript
简单实现jQuery弹窗效果
2017/10/30 jQuery
深入剖析Node.js cluster模块
2018/05/23 Javascript
如何解决.vue文件url引用文件的问题
2019/01/18 Javascript
Vue插件从封装到发布的完整步骤记录
2019/02/28 Javascript
如何用webpack4.0撸单页/多页脚手架 (jquery, react, vue, typescript)
2019/06/18 jQuery
react koa rematch 如何打造一套服务端渲染架子
2019/06/26 Javascript
一篇文章看懂JavaScript中的回调
2021/01/05 Javascript
python和bash统计CPU利用率的方法
2015/07/10 Python
Python实现破解12306图片验证码的方法分析
2017/12/29 Python
浅谈pycharm的xmx和xms设置方法
2018/12/03 Python
浅谈python requests 的put, post 请求参数的问题
2019/01/02 Python
python解析含有重复key的json方法
2019/01/22 Python
Python属性和内建属性实例解析
2020/01/14 Python
mac 上配置Pycharm连接远程服务器并实现使用远程服务器Python解释器的方法
2020/03/19 Python
用python写爬虫简单吗
2020/07/28 Python
新加坡网上化妆品店:Best Buy World
2018/05/18 全球购物
JBL澳大利亚官方商店:扬声器、耳机和音响系统
2018/05/24 全球购物
可持续未来的时尚基础:Alternative Apparel
2019/05/06 全球购物
德国苹果商店:MacTrade
2020/05/18 全球购物
行政部总经理岗位职责
2014/01/04 职场文书
幼儿教师师德演讲稿
2014/05/06 职场文书
常务副总经理任命书
2014/06/05 职场文书
旷工检讨书1000字
2015/01/01 职场文书
银行反洗钱宣传活动总结
2015/05/08 职场文书
郭明义电影观后感
2015/06/08 职场文书
css 中多种边框的实现小窍门
2021/04/07 HTML / CSS