php实现单链表的实例代码


Posted in PHP onMarch 22, 2013
<?php
//链表节点 
class node { 
    public $id; //节点id 
    public $name; //节点名称 
    public $next; //下一节点 
    
    public function __construct($id, $name) { 
        $this->id = $id; 
        $this->name = $name; 
        $this->next = null; 
    } 
}
//单链表 
class singelLinkList { 
    private $header; //链表头节点 
    
    //构造方法 
    public function __construct($id = null, $name = null) { 
        $this->header = new node ( $id, $name, null ); 
    } 
    //获取链表长度 
    public function getLinkLength() { 
        $i = 0; 
        $current = $this->header; 
        while ( $current->next != null ) { 
            $i ++; 
            $current = $current->next; 
        } 
        return $i; 
    } 
    //添加节点数据 
    public function addLink($node) { 
        $current = $this->header; 
        while ( $current->next != null ) { 
            if ($current->next->id > $node->id) { 
                break; 
            } 
            $current = $current->next; 
        } 
        $node->next = $current->next; 
        $current->next = $node; 
    } 
    //删除链表节点 
    public function delLink($id) { 
        $current = $this->header; 
        $flag = false; 
        while ( $current->next != null ) { 
            if ($current->next->id == $id) { 
                $flag = true; 
                break; 
            } 
            $current = $current->next; 
        } 
        if ($flag) { 
            $current->next = $current->next->next; 
        } else { 
            echo "未找到id=" . $id . "的节点!<br>"; 
        } 
    } 
    //获取链表 
    public function getLinkList() { 
        $current = $this->header; 
        if ($current->next == null) { 
            echo ("链表为空!"); 
            return; 
        } 
        while ( $current->next != null ) { 
            echo 'id:' . $current->next->id . '   name:' . $current->next->name . "<br>"; 
            if ($current->next->next == null) { 
                break; 
            } 
            $current = $current->next; 
        } 
    } 
    //获取节点名字 
    public function getLinkNameById($id) { 
        $current = $this->header; 
        if ($current->next == null) { 
            echo "链表为空!"; 
            return; 
        } 
        while ( $current->next != null ) { 
            if ($current->id == $id) { 
                break; 
            } 
            $current = $current->next; 
        } 
        return $current->name; 
    } 
    //更新节点名称 
    public function updateLink($id, $name) { 
        $current = $this->header; 
        if ($current->next == null) { 
            echo "链表为空!"; 
            return; 
        } 
        while ( $current->next != null ) { 
            if ($current->id == $id) { 
                break; 
            } 
            $current = $current->next; 
        } 
        return $current->name = $name; 
    } 
}
$lists = new singelLinkList (); 
$lists->addLink ( new node ( 5, 'eeeeee' ) ); 
$lists->addLink ( new node ( 1, 'aaaaaa' ) ); 
$lists->addLink ( new node ( 6, 'ffffff' ) ); 
$lists->addLink ( new node ( 4, 'dddddd' ) ); 
$lists->addLink ( new node ( 3, 'cccccc' ) ); 
$lists->addLink ( new node ( 2, 'bbbbbb' ) ); 
$lists->getLinkList (); 
echo "<br>-----------删除节点--------------<br>"; 
$lists->delLink ( 5 ); 
$lists->getLinkList ();
echo "<br>-----------更新节点名称--------------<br>"; 
$lists->updateLink ( 3, "222222" ); 
$lists->getLinkList ();
echo "<br>-----------获取节点名称--------------<br>"; 
echo $lists->getLinkNameById ( 5 );
echo "<br>-----------获取链表长度--------------<br>"; 
echo $lists->getLinkLength (); 
?>
PHP 相关文章推荐
基于curl数据采集之正则处理函数get_matches的使用
Apr 28 PHP
PHP中使用Session配合Javascript实现文件上传进度条功能
Oct 15 PHP
php递归创建目录的方法
Feb 02 PHP
PHP多文件上传类实例
Mar 07 PHP
php简单实现多字节字符串翻转的方法
Mar 31 PHP
php将远程图片保存到本地服务器的实现代码
Aug 03 PHP
php生成动态验证码gif图片
Oct 19 PHP
php实用代码片段整理
Nov 12 PHP
启用OPCache提高PHP程序性能的方法
Mar 21 PHP
thinkphp 5框架实现登陆,登出及session登陆状态检测功能示例
Oct 10 PHP
PHP实现随机发放扑克牌
Apr 21 PHP
基于PHP实现解密或加密Cloudflar邮箱保护
Jun 24 PHP
php 判断数组是几维数组
Mar 20 #PHP
php页面消耗内存过大的处理办法
Mar 18 #PHP
ajax取消挂起请求的处理方法
Mar 18 #PHP
smarty 缓存控制前的页面静态化原理
Mar 15 #PHP
PHP中使用cURL实现Get和Post请求的方法
Mar 13 #PHP
php文本转图片自动换行的方法
Mar 13 #PHP
用Php编写注册后Email激活验证的实例代码
Mar 11 #PHP
You might like
综合图片计数器
2006/10/09 PHP
php xml 入门学习资料
2011/01/01 PHP
ThinkPHP模型详解
2015/07/27 PHP
Yii2.0中的COOKIE和SESSION用法
2016/08/12 PHP
php实现登录页面的简单实例
2019/09/29 PHP
js树形控件脚本代码
2008/07/24 Javascript
JS实现仿Windows7风格的网页右键菜单效果代码
2015/09/11 Javascript
js+ajax实现获取文件大小的方法
2015/12/08 Javascript
jQuery实现的指纹扫描效果实例(附演示与demo源码下载)
2016/01/26 Javascript
用NodeJS实现批量查询地理位置的经纬度接口
2016/08/16 NodeJs
利用Angularjs和Bootstrap前端开发案例实战
2016/08/27 Javascript
鼠标经过出现气泡框的简单实例
2017/03/17 Javascript
React教程之封装一个Portal可复用组件的方法
2018/01/02 Javascript
React中如何引入Angular组件详解
2018/08/09 Javascript
vue实现多个元素或多个组件之间动画效果
2018/09/25 Javascript
JavaScript数据结构与算法之检索算法实例分析【顺序查找、最大最小值、自组织查询】
2019/02/22 Javascript
vue2.0结合Element-ui实战案例
2019/03/06 Javascript
原生JS实现随机点名项目的实例代码
2019/04/30 Javascript
JS实现网页端猜数字小游戏
2020/03/06 Javascript
json.stringify()与json.parse()的区别以及用处
2021/01/25 Javascript
[05:08]2014DOTA2国际邀请赛 Hao专访复仇的胜利很爽
2014/07/15 DOTA
python3简单实现微信爬虫
2015/04/09 Python
Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项
2018/11/30 Python
对python当中不在本路径的py文件的引用详解
2018/12/15 Python
python图的深度优先和广度优先算法实例分析
2019/10/26 Python
容易被忽略的Python内置类型
2020/09/03 Python
Python3.7安装PyQt5 运行配置Pycharm的详细教程
2020/10/15 Python
CSS3弹性盒模型开发笔记(二)
2016/04/26 HTML / CSS
英国人最爱的饰品网站:Accessorize
2016/08/22 全球购物
英国领先的在线药房:Pharmacy First
2017/09/10 全球购物
国贸类专业毕业生的求职信分享
2013/12/08 职场文书
十八届三中全会报告学习材料
2014/02/17 职场文书
伦敦奥运会的口号
2014/06/21 职场文书
2014第二批党的群众路线教育实践活动对照检查材料思想汇报
2014/09/18 职场文书
如果用一句诗总结你的上半年,你会用哪句呢?
2019/07/16 职场文书
pytorch实现ResNet结构的实例代码
2021/05/17 Python