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 和 MySQL 开发的 8 个技巧
Oct 09 PHP
如何在PHP中使用Oracle数据库(2)
Oct 09 PHP
聊天室php&amp;mysql(二)
Oct 09 PHP
php新建文件自动编号的思路与实现
Jun 27 PHP
php实现分页工具类分享
Jan 09 PHP
写一段简单的PHP建立文件夹代码
Jan 06 PHP
PHP简单的MVC框架实现方法
Dec 01 PHP
php实现购物车功能(下)
Jan 05 PHP
php while循环控制的简单实例
May 30 PHP
Ajax实现对静态页面的文章访问统计功能示例
Oct 10 PHP
PHP利用Socket获取网站的SSL证书与公钥
Jun 18 PHP
PHP rsa加密解密算法原理解析
Dec 09 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/12/03 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
SyntaxHighlighter语法高亮插件使用说明
2011/08/14 Javascript
在网站上应该用的30个jQuery插件整理
2011/11/03 Javascript
javascript拖拽上传类库DropzoneJS使用方法
2013/12/05 Javascript
Jquery使用css方法改变样式实例
2015/05/18 Javascript
ECMAScript6中Set/WeakSet详解
2015/06/12 Javascript
js实现创建删除html元素小结
2015/09/30 Javascript
微信小程序 弹窗自定义实例代码
2017/03/08 Javascript
Vue实现virtual-dom的原理简析
2017/07/10 Javascript
详解使用jQuery.i18n.properties实现js国际化
2018/05/04 jQuery
JS实现中英文混合文字溢出友好截取功能
2018/08/06 Javascript
Vue实现6位数密码效果
2018/08/18 Javascript
JS实现Cookie读、写、删除操作工具类示例
2018/08/28 Javascript
three.js搭建室内场景教程
2018/12/30 Javascript
layer关闭当前窗口页面以及确认取消按钮的方法
2019/09/09 Javascript
解决layui页面按钮点击无反应,也不报错的问题
2019/09/29 Javascript
在vue中使用inheritAttrs实现组件的扩展性介绍
2020/12/07 Vue.js
[02:50]2014DOTA2 TI预选赛预选赛 大神专访第一弹!
2014/05/21 DOTA
python通过cookie模拟已登录状态的初步研究
2016/11/09 Python
Python探索之Metaclass初步了解
2017/10/28 Python
Python程序运行原理图文解析
2018/02/10 Python
python微信跳一跳系列之色块轮廓定位棋盘
2018/02/26 Python
详解PyTorch手写数字识别(MNIST数据集)
2019/08/16 Python
总结python 三种常见的内存泄漏场景
2020/11/20 Python
使用JS+CSS3技术:让你的名字动起来
2013/04/27 HTML / CSS
美国在线自行车商店:Jenson USA
2018/05/22 全球购物
印尼综合在线预订网站:Tiket.com(机票、酒店、火车、租车和娱乐)
2018/10/11 全球购物
如何利用XMLHTTP检测URL及探测服务器信息
2013/11/10 面试题
社会调查研究计划书
2014/05/01 职场文书
商场客服专员岗位职责
2014/06/13 职场文书
小学数学课题方案
2014/06/15 职场文书
个人作风建设自查报告
2014/10/22 职场文书
教你快速构建一个基于nginx的web集群项目
2021/11/27 Servers
基于PostgreSQL/openGauss 的分布式数据库解决方案
2021/12/06 PostgreSQL
Redis基本数据类型List常用操作命令
2022/06/01 Redis