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 程式大小
Dec 06 PHP
Uncaught exception com_exception with message Failed to create COM object
Jan 11 PHP
探讨各种PHP字符串函数的总结分析
Jun 05 PHP
PHP基于CURL进行POST数据上传实例
Nov 10 PHP
Linux中为php配置伪静态
Dec 17 PHP
php实现上传图片保存到数据库的方法
Feb 11 PHP
PHP批量查询WordPress留言者E-mail地址实现方法
Feb 15 PHP
解决更换PHP5.4以上版本后Dedecms后台登录空白问题的方法
Oct 23 PHP
Yii2简单实现多语言配置的方法
Jul 23 PHP
利用php做服务器和web前端的界面进行交互
Oct 31 PHP
基于Laravel5.4实现多字段登录功能方法示例
Aug 11 PHP
Laravel框架分页实现方法分析
Jun 12 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实现图片裁剪、添加水印效果代码
2014/10/01 PHP
php上传文件问题汇总
2015/01/30 PHP
js获取提交的字符串的字节数
2009/02/09 Javascript
javascript实现分栏显示小技巧附图
2014/10/13 Javascript
Javascript 中创建自定义对象的方法汇总
2014/12/04 Javascript
jquery动态添加删除(tr/td)
2015/02/09 Javascript
javascript中typeof操作符和constucor属性检测
2015/02/26 Javascript
JS组件Bootstrap Select2使用方法详解
2020/04/17 Javascript
Node.js本地文件操作之文件拷贝与目录遍历的方法
2016/02/16 Javascript
JS判断图片是否加载完成方法汇总(最新版)
2016/05/13 Javascript
基于Bootstrap3表格插件和分页插件实例详解
2016/05/17 Javascript
关于javascript原型的修改与重写(覆盖)差别详解
2016/08/31 Javascript
Bootstrap实现基于carousel.js框架的轮播图效果
2017/05/02 Javascript
Js实现京东无延迟菜单效果实例(demo)
2017/06/02 Javascript
浅谈ECMAScript6新特性之let、const
2017/08/02 Javascript
js构建二叉树进行数值数组的去重与优化详解
2018/03/26 Javascript
浅谈Vue组件及组件的注册方法
2018/08/24 Javascript
微信小程序自定义toast的实现代码
2018/11/16 Javascript
layui使用button按钮 点击出现弹层 弹层中加载表单的实例
2019/09/04 Javascript
python中的列表推导浅析
2014/04/26 Python
Python 字典dict使用介绍
2014/11/30 Python
Django的URLconf中使用缺省视图参数的方法
2015/07/18 Python
python3实现暴力穷举博客园密码
2016/06/19 Python
Python中不同进制的语法及转换方法分析
2016/07/27 Python
详解python中xlrd包的安装与处理Excel表格
2016/12/16 Python
python数据结构学习之实现线性表的顺序
2018/09/28 Python
Python 实现中值滤波、均值滤波的方法
2019/01/09 Python
Python中如何使用if语句处理列表实例代码
2019/02/24 Python
python绘制雪景图
2019/12/16 Python
营业员个人总结的自我评价
2013/10/25 职场文书
春风行动实施方案
2014/03/28 职场文书
小学一年级数学教学计划
2015/01/20 职场文书
索赔员岗位职责
2015/02/15 职场文书
写给同事的离职感言
2015/08/04 职场文书
2019年关于小学生课外阅读情况的分析报告
2019/12/02 职场文书
简单且有用的Python数据分析和机器学习代码
2021/07/02 Python