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 相关文章推荐
如何去掉文章里的 html 语法
Oct 09 PHP
PHP入门
Oct 09 PHP
修改了一个很不错的php验证码(支持中文)
Feb 14 PHP
php pcntl_fork和pcntl_fork 的用法
Apr 13 PHP
php 获取全局变量的代码
Apr 21 PHP
php中time()和mktime()方法的区别
Sep 28 PHP
php中操作memcached缓存进行增删改查数据的实现代码
Aug 15 PHP
浅谈thinkphp的实例化模型
Jan 04 PHP
PHP中使用break跳出多重循环代码实例
Jan 21 PHP
PHP查看SSL证书信息的方法
Sep 22 PHP
[原创]PHP正则匹配中英文、数字及下划线的方法【用户名验证】
Aug 01 PHP
利用PHP实现开心消消乐的算法示例
Oct 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 Coding Tips(php小技巧)[2011/04/02最后更新]
2011/05/02 PHP
解析php php_openssl.dll的作用
2013/07/01 PHP
php设计模式之组合模式实例详解【星际争霸游戏案例】
2020/03/27 PHP
关闭浏览器窗口弹出提示框并且可以控制其失效
2014/04/15 Javascript
禁止按回车键提交表单的方法
2015/06/11 Javascript
ES6中非常实用的新特性介绍
2016/03/10 Javascript
jquery分页插件jquery.pagination.js实现无刷新分页
2016/04/01 Javascript
使用JavaScript实现链表的数据结构的代码
2017/08/02 Javascript
Js中将Long转换成日期格式的实现方法
2018/06/05 Javascript
深入分析element ScrollBar滚动组件源码
2019/01/22 Javascript
jQuery实现鼠标移入显示蒙版效果
2020/01/11 jQuery
python冒泡排序算法的实现代码
2013/11/21 Python
python使用reportlab实现图片转换成pdf的方法
2015/05/22 Python
Python使用正则表达式过滤或替换HTML标签的方法详解
2017/09/25 Python
Python实现PS图像明亮度调整效果示例
2018/01/23 Python
Python实现带参数的用户验证功能装饰器示例
2018/12/14 Python
利用python实现简易版的贪吃蛇游戏(面向python小白)
2018/12/30 Python
Python利用itchat库向好友或者公众号发消息的实例
2019/02/21 Python
Python3 文章标题关键字提取的例子
2019/08/26 Python
python3获取控制台输入的数据的具体实例
2020/08/16 Python
amazeui页面分析之登录页面的示例代码
2020/08/25 HTML / CSS
美国在线自行车商店:Jenson USA
2018/05/22 全球购物
Dr.Jart+美国官网:韩国药妆品牌
2019/01/18 全球购物
倩碧澳大利亚官网:Clinique澳大利亚
2019/07/22 全球购物
荷兰网上药店:Drogisterij.net
2019/09/03 全球购物
《小蝌蚪找妈妈》教学反思
2014/02/21 职场文书
祖国在我心中演讲稿500字
2014/05/04 职场文书
2014年十八届四中全会思想汇报范文
2014/10/17 职场文书
2014年检验员工作总结
2014/11/19 职场文书
文明单位创建材料
2014/12/24 职场文书
2015年体育部工作总结
2015/04/02 职场文书
感动中国何玥观后感
2015/06/02 职场文书
律政俏佳人观后感
2015/06/09 职场文书
如何书写公司员工保密协议?
2019/06/27 职场文书
python批量创建变量并赋值操作
2021/06/03 Python
webpack介绍使用配置教程详解webpack介绍和使用
2022/06/25 Javascript