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 相关文章推荐
PHP4和PHP5性能测试和对比 测试代码与环境
Aug 17 PHP
PHP 程序授权验证开发思路
Jul 09 PHP
php 各种应用乱码问题的解决方法
May 09 PHP
php增删改查示例自己写的demo
Sep 04 PHP
php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
Sep 28 PHP
PHP中判断变量为空的几种方法小结
Nov 12 PHP
php实现俄罗斯乘法实例
Mar 07 PHP
PHP实现在线阅读PDF文件的方法
Jun 23 PHP
php实现将Session写入数据库
Jul 26 PHP
PHP中addcslashes与stripcslashes函数用法分析
Jan 07 PHP
PHP使用PDO操作数据库的乱码问题解决方法
Apr 08 PHP
PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
Apr 11 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
解决MySQL中文输出变成问号的问题
2008/06/05 PHP
PHP程序61条面向对象分析设计的经验小结
2008/11/12 PHP
PHP中的错误处理、异常处理机制分析
2012/05/07 PHP
PHP制作万年历
2015/01/07 PHP
PHP数学运算与数据处理实例分析
2016/04/01 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
js特效,页面下雪的小例子
2013/06/17 Javascript
jQuery 文本框得失焦点的简单实例
2014/02/19 Javascript
有效提高JavaScript执行效率的几点知识
2015/01/31 Javascript
Javascript实现飞动广告效果的方法
2015/05/25 Javascript
jquery插件autocomplete用法示例
2016/07/01 Javascript
jQuery.ajax 跨域请求webapi设置headers的解决方案
2016/08/08 Javascript
Ionic默认的Tabs模板使用实例
2016/08/29 Javascript
AngularJS通过$http和服务器通信详解
2016/09/21 Javascript
微信小程序开发之toast等弹框提示使用教程
2017/06/08 Javascript
jQuery 实现左右两侧菜单添加、移除功能
2018/01/02 jQuery
nodejs中Express与Koa2对比分析
2018/02/06 NodeJs
[01:04:06]DOTA2上海特级锦标赛A组资格赛#2 Secret VS EHOME第一局
2016/02/26 DOTA
Python import用法以及与from...import的区别
2015/05/28 Python
pandas获取groupby分组里最大值所在的行方法
2018/04/20 Python
关于Python 常用获取元素 Driver 总结
2019/11/24 Python
python 装饰器功能与用法案例详解
2020/03/06 Python
全球知名的珠宝首饰品牌:Kay Jewelers
2018/02/11 全球购物
Booking.com缤客中国:全球酒店在线预订网站
2020/05/03 全球购物
.net C#面试题
2012/08/28 面试题
一名女生的自荐信
2013/12/08 职场文书
电气自动化个人求职信范文
2014/02/03 职场文书
求职信结尾怎么写
2014/05/26 职场文书
2015年初中元旦晚会活动总结
2014/11/28 职场文书
学术会议邀请函
2015/01/30 职场文书
2015年暑期社会实践活动总结
2015/03/27 职场文书
2015年国培研修感言
2015/08/01 职场文书
小学生大队委竞选稿
2015/11/20 职场文书
2016优秀毕业生个人事迹材料
2016/02/29 职场文书
Nginx进程管理和重载原理详解
2021/04/22 Servers
nginx共享内存的机制详解
2022/03/21 Servers