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 相关文章推荐
PHP初学入门
Nov 19 PHP
刚才在简化php的库,结果发现很多东西
Dec 31 PHP
PHP获取当前文件所在目录 getcwd()函数
May 13 PHP
php cURL和Rolling cURL并发方式比较
Oct 30 PHP
php使用date和strtotime函数输出指定日期的方法
Nov 14 PHP
php自动给网址加上链接的方法
Jun 02 PHP
php链表用法实例分析
Jul 09 PHP
PHP Yii框架之表单验证规则大全
Nov 16 PHP
PHP实现动态添加XML中数据的方法
Mar 30 PHP
在Laravel中使用DataTables插件的方法
May 29 PHP
laravel 实现根据字段不同值做不同查询
Oct 23 PHP
Laravel 框架基于自带的用户系统实现登录注册及错误处理功能分析
Apr 14 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
如何使用PHP获取网络上文件
2006/10/09 PHP
php数组查找函数总结
2014/11/18 PHP
让Laravel API永远返回JSON格式响应的方法示例
2018/09/05 PHP
“不能执行已释放的Script代码”错误的原因及解决办法
2007/09/09 Javascript
fireworks菜单生成器mm_menu.js在 IE 7.0 显示问题的解决方法
2009/10/20 Javascript
javascript options属性集合操作代码
2009/12/28 Javascript
Javascript面向对象扩展库代码分享
2012/03/27 Javascript
js固定DIV高度,超出部分自动添加滚动条的简单方法
2013/07/10 Javascript
javascript打开word文档的方法
2014/04/16 Javascript
jquery获取tagName再进行判断
2014/05/29 Javascript
javascript获取指定区间范围随机数的方法
2017/09/08 Javascript
element-ui 设置菜单栏展开的方法
2018/08/22 Javascript
Vuex 使用及简单实例(计数器)
2018/08/29 Javascript
解决百度Echarts图表坐标轴越界的方法
2018/10/17 Javascript
Nodejs中获取当前函数被调用的行数及文件名详解
2018/12/12 NodeJs
简单学习5种处理Vue.js异常的方法
2019/06/17 Javascript
Python中的tuple元组详细介绍
2015/02/02 Python
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
Python数据分析pandas模块用法实例详解
2019/11/20 Python
python使用正则表达式(Regular Expression)方法超详细
2019/12/30 Python
手动安装python3.6的操作过程详解
2020/01/13 Python
Python创建空列表的字典2种方法详解
2020/02/13 Python
python设置环境变量的作用整理
2020/02/17 Python
Django中Q查询及Q()对象 F查询及F()对象用法
2020/07/09 Python
JupyterNotebook 输出窗口的显示效果调整实现
2020/09/22 Python
通过CSS3的object-fit来调整图片适配尺寸的技巧简介
2016/02/27 HTML / CSS
美国知名的女性服饰品牌:LOFT(洛芙特)
2016/08/05 全球购物
医药大学生求职简历的自我评价
2013/10/17 职场文书
大学毕业后的十年规划
2014/01/07 职场文书
小孩百日宴答谢词
2014/01/15 职场文书
元旦红领巾广播稿
2014/02/19 职场文书
商业房地产广告语
2014/03/13 职场文书
单位租房协议范本
2014/12/03 职场文书
大学毕业论文致谢词
2015/05/14 职场文书
Python 详解通过Scrapy框架实现爬取CSDN全站热榜标题热词流程
2021/11/11 Python
Oracle删除归档日志及添加定时任务
2022/06/28 Oracle