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的栏目导航程序
Oct 09 PHP
PHP stream_context_create()作用和用法分析
Mar 29 PHP
PHP中将数组转成XML格式的实现代码
Aug 08 PHP
php过滤XSS攻击的函数
Nov 12 PHP
thinkphp循环结构用法实例
Nov 24 PHP
将PHP从5.3.28升级到5.3.29时Nginx出现502错误
May 09 PHP
PHP符合PSR编程规范的实例分享
Dec 21 PHP
php连接mysql数据库
Mar 21 PHP
php JWT在web端中的使用方法教程
Sep 06 PHP
php转换上传word文件为PDF的方法【基于COM组件】
Jun 10 PHP
Thinkphp极验滑动验证码实现步骤解析
Nov 24 PHP
php array_map()函数实例用法
Mar 03 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
在任意字符集下正常显示网页的方法一
2007/04/01 PHP
php设计模式之工厂模式用法经典实例分析
2019/09/20 PHP
accesskey 提交
2006/06/26 Javascript
IE6、IE7中获取Button元素的值的bug说明
2011/08/28 Javascript
js中iframe调用父页面的方法
2014/10/30 Javascript
javascript的函数作用域
2014/11/12 Javascript
JQuery显示、隐藏div的几种方法简明总结
2015/04/16 Javascript
jQuery判断checkbox选中状态
2016/05/12 Javascript
分享jQuery网页元素拖拽插件
2020/12/01 Javascript
微信jssdk用法汇总
2016/07/16 Javascript
JS在Chrome浏览器中showModalDialog函数返回值为undefined的解决方法
2016/08/03 Javascript
Query常用DIV操作获取和设置长度宽度的实现方法
2016/09/19 Javascript
jQuery通过ajax快速批量提交表单数据
2016/10/25 Javascript
jQuery插件zTree实现清空选中第一个节点所有子节点的方法
2017/03/08 Javascript
nodejs socket服务端和客户端简单通信功能
2017/09/14 NodeJs
vue组件编写之todolist组件实例详解
2018/01/22 Javascript
vue cli构建的项目中请求代理与项目打包问题
2018/02/26 Javascript
微信小程序-form表单提交代码实例
2019/04/29 Javascript
浅谈Vue为什么不能检测数组变动
2019/10/14 Javascript
Electron 打包问题:electron-builder 下载各种依赖出错(推荐)
2020/07/09 Javascript
了解不常见但是实用的Python技巧
2019/05/23 Python
PyCharm搭建Spark开发环境实现第一个pyspark程序
2019/06/13 Python
Python实现在线批量美颜功能过程解析
2020/06/10 Python
Python导入父文件夹中模块并读取当前文件夹内的资源
2020/11/19 Python
Topshop法国官网:英国快速时尚品牌
2018/04/08 全球购物
下列程序在32位linux或unix中的结果是什么
2014/03/25 面试题
销售行业个人求职自荐信
2013/09/25 职场文书
公关活动策划方案
2014/05/25 职场文书
国庆横幅标语
2014/10/08 职场文书
校园文化艺术节宣传标语
2014/10/09 职场文书
写景作文评语集锦
2014/12/25 职场文书
端午节活动总结报告
2015/02/11 职场文书
2015年控辍保学工作总结
2015/05/18 职场文书
Go语言空白表示符_的实例用法
2021/07/04 Golang
入门学习Go的基本语法
2021/07/07 Golang
Redis模仿手机验证码发送的实现示例
2021/11/02 Redis