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脚本的10个技巧(6)
Oct 09 PHP
杏林同学录(七)
Oct 09 PHP
ExtJS与PHP、MySQL实现存储的方法
Apr 02 PHP
php下载文件的代码示例
Jun 29 PHP
基于PHP开发中的安全防范知识详解
Jun 06 PHP
关于php循环跳出的问题
Jul 01 PHP
php动态生成函数示例
Mar 21 PHP
神盾加密解密教程(一)PHP变量可用字符
May 28 PHP
PHP打开和关闭文件操作函数总结
Nov 18 PHP
Zend Framework开发入门经典教程
Mar 23 PHP
PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析
Jul 22 PHP
thinkphp5 加载静态资源路径与常量的方法
Dec 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
php SQL防注入代码集合
2008/04/25 PHP
php 代码优化之经典示例
2011/03/24 PHP
thinkphp的dump函数无输出实例代码
2016/11/15 PHP
thinkphp5框架扩展redis类方法示例
2019/05/06 PHP
jquery 表单下所有元素的隐藏
2009/07/25 Javascript
JQuery 引发两次$(document.ready)事件
2010/01/15 Javascript
Jquery iframe内部出滚动条
2010/02/11 Javascript
JS运行耗时操作的延时显示方法
2010/11/19 Javascript
Jquery中Ajax 缓存带来的影响的解决方法
2011/05/19 Javascript
三种方式获取XMLHttpRequest对象
2014/04/21 Javascript
setInterval计时器不准的问题解决方法
2014/05/08 Javascript
Node.js开发之访问Redis数据库教程
2015/01/14 Javascript
JavaScript实现Iterator模式实例分析
2015/06/09 Javascript
判断JS对象是否拥有某属性的方法推荐
2016/05/12 Javascript
JavaScript中push(),join() 函数 实例详解
2016/09/06 Javascript
微信JSSDK调用微信扫一扫功能的方法
2017/07/25 Javascript
element-ui多文件上传的实现示例
2019/04/10 Javascript
基于vue实现一个禅道主页拖拽效果
2019/05/27 Javascript
vue按需加载实例详解
2019/09/06 Javascript
vue使用svg文件补充-svg放大缩小操作(使用d3.js)
2020/09/22 Javascript
python 实现读取一个excel多个sheet表并合并的方法
2019/02/12 Python
对Python3 goto 语句的使用方法详解
2019/02/16 Python
python 进程的几种创建方式详解
2019/08/29 Python
Django自定义列表 models字段显示方式
2020/04/03 Python
利用纯html5绘制出来的一款非常漂亮的时钟
2015/01/04 HTML / CSS
KIKO MILANO荷兰网上商店:意大利专业化妆品品牌
2017/05/12 全球购物
中国专业的音频分享平台:喜马拉雅
2019/05/24 全球购物
俄罗斯在线大型超市:ТутПросто
2021/01/08 全球购物
什么时候需要进行强制类型转换
2016/09/03 面试题
幼儿园中秋节活动方案2013
2014/01/29 职场文书
政法学院毕业生求职信
2014/02/28 职场文书
《沙漠中的绿洲》教学反思
2014/04/24 职场文书
小学生2014国庆节演讲稿:祖国在我心中
2014/09/21 职场文书
2015年人事科工作总结
2015/04/28 职场文书
2015年高中班级工作总结
2015/07/21 职场文书
早安问候语大全
2015/11/10 职场文书