PHP小教程之实现双向链表


Posted in PHP onJune 12, 2014

看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下。上一次分享了《PHP小教程之实现链表》,这次来补充说一下双向链表。

<?php
        class Hero
        {
            public $pre=null;
            public $no;
            public $name;
            public $next=null;
            public function __construct($no='',$name='')
            {
                $this->no=$no;
                $this->name=$name;
            }
            static public function addHero($head,$hero)
            {
                $cur = $head;
                $isExist=false;
                //判断目前这个链表是否为空
                if($cur->next==null)
                {
                    $cur->next=$hero;
                    $hero->pre=$cur;
                }
                else
                {
                    //如果不是空节点,则安排名来添加
                    //找到添加的位置
                    while($cur->next!=null)
                    {
                        if($cur->next->no > $hero->no)
                        {
                            break;
                        }
                        else if($cur->next->no == $hero->no)
                        {
                            $isExist=true;
                            echo "<br>不能添加相同的编号";
                        }
                        $cur=$cur->next;
                    }
                    if(!$isExist)
                    {
                        if($cur->next!=null)
                        {
                            $hero->next=$cur->next;
                        }
                        $hero->pre=$cur;
                        if($cur->next!=null)
                        {
                            $hero->next->pre=$hero;
                        }
                        $cur->next=$hero;                    
                    }
                }
            }
            //遍历
            static public function showHero($head)
            {
                $cur=$head;
                while($cur->next!=null)
                {
                    echo "<br>编号:".$cur->next->no."名字:".$cur->next->name;
                    $cur=$cur->next;
                }
            }
            static public function delHero($head,$herono)
            {
                $cur=$head;
                $isFind=false;
                while($cur!=null)
                {
                    if($cur->no==$herono)
                    {
                        $isFind=true;
                        break;
                    }
                    //继续找
                    $cur=$cur->next;
                }
                if($isFind)
                {
                    if($cur->next!=null)
                    {
                        $cur->next_pre=$cur->pre;
                    }
                    $cur->pre->next=$cur->next;
                }
                else
                {
                    echo "<br>没有找到目标";
                }                
            }
        }
        $head = new Hero();
        $hero1 = new Hero(1,'1111');
        $hero3 = new Hero(3,'3333');
        $hero2 = new Hero(2,'2222');
        Hero::addHero($head,$hero1);
        Hero::addHero($head,$hero3);
        Hero::addHero($head,$hero2);
        Hero::showHero($head);
        Hero::delHero($head,2);
        Hero::showHero($head);
?>
PHP 相关文章推荐
在PHP中执行系统外部命令
Oct 09 PHP
PHP Global定义全局变量使用说明
Aug 15 PHP
php cURL和Rolling cURL并发方式比较
Oct 30 PHP
php接口和抽象类使用示例详解
Mar 02 PHP
php写入数据到CSV文件的方法
Mar 14 PHP
php线性表的入栈与出栈实例分析
Jun 12 PHP
php验证码实现代码(3种)
Sep 07 PHP
PHP中对数组的一些常用的增、删、插操作函数总结
Nov 27 PHP
php 浮点数比较方法详解
May 05 PHP
CI框架附属类用法分析
Dec 26 PHP
Laravel使用Queue队列的技巧汇总
Sep 02 PHP
Laravel使用模型实现like模糊查询的例子
Oct 24 PHP
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
Jun 12 #PHP
PHP命名空间(Namespace)简明教程
Jun 11 #PHP
PHP APC配置文件2套和参数详解
Jun 11 #PHP
PHP捕获Fatal error错误的方法
Jun 11 #PHP
PHP独立Session数据库存储操作类分享
Jun 11 #PHP
php调用nginx的mod_zip模块打包ZIP文件
Jun 11 #PHP
php+ajax导入大数据时产生的问题处理
Jun 11 #PHP
You might like
解析PHP中的file_get_contents获取远程页面乱码的问题
2013/06/25 PHP
phpstrom使用xdebug配置方法
2013/12/17 PHP
PHP设计模式之适配器模式定义与用法详解
2018/04/03 PHP
java解析json方法总结
2019/05/16 PHP
PHP大文件分块上传功能实例详解
2019/07/22 PHP
JavaScript中的私有成员
2006/09/18 Javascript
浅谈JavaScript中面向对象技术的模拟
2006/09/25 Javascript
用javascript作一个通用向导说明
2011/08/30 Javascript
jquery二级导航内容均分的原理及实现
2013/08/13 Javascript
浅谈JavaScript的Polymer框架中的behaviors对象
2015/07/29 Javascript
深入浅析Bootstrap列表组组件
2016/05/03 Javascript
Bootstrap组合上、下拉框简单实现代码
2017/03/06 Javascript
es6的数字处理的方法(5个)
2017/03/16 Javascript
python实现的解析crontab配置文件代码
2014/06/30 Python
Python入门篇之数字
2014/10/20 Python
Python学生信息管理系统修改版
2018/03/13 Python
python matplotlib 在指定的两个点之间连线方法
2018/05/25 Python
python中p-value的实现方式
2019/12/16 Python
Python sqlite3查询操作过程解析
2020/02/20 Python
解决Python pip 自动更新升级失败的问题
2020/02/21 Python
python 成功引入包但无法正常调用的解决
2020/03/09 Python
Python使用sqlite3模块内置数据库
2020/05/07 Python
Python中SQLite如何使用
2020/05/27 Python
Python学习之os模块及用法
2020/06/03 Python
用Python进行websocket接口测试
2020/10/16 Python
CSS3中几个新增加的盒模型属性使用教程
2016/03/01 HTML / CSS
html5中svg canvas和图片之间相互转化思路代码
2014/01/24 HTML / CSS
localStorage、sessionStorage使用总结
2017/11/17 HTML / CSS
html5实现滑块功能之type=&quot;range&quot;属性
2020/02/18 HTML / CSS
同学会邀请书大全
2014/01/12 职场文书
学生自我评语大全
2014/04/18 职场文书
乡镇消防安全责任书
2014/07/23 职场文书
生活部的活动方案
2014/08/19 职场文书
2014年小班保育员工作总结
2014/12/23 职场文书
大学生村官个人总结
2015/02/15 职场文书
员工拾金不昧表扬稿
2015/05/05 职场文书