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 相关文章推荐
一个MYSQL操作类
Nov 16 PHP
php生成的html meta和link标记在body标签里 顶部有个空行
May 18 PHP
PHP中error_reporting()函数的用法(修改PHP屏蔽错误)
Jul 01 PHP
基于php设计模式中单例模式的应用分析
May 15 PHP
PHP遍历某个目录下的所有文件和子文件夹的实现代码
Jun 28 PHP
php的XML文件解释类应用实例
Sep 22 PHP
Php-Redis安装测试笔记
Mar 05 PHP
php使用simplexml_load_file加载XML文件并显示XML的方法
Mar 19 PHP
php实现mysql数据库分表分段备份
Jun 18 PHP
PHP使用Pear发送邮件(Windows环境)
Jan 05 PHP
PHP使用POP3读取邮箱接收邮件的示例代码
Jul 08 PHP
通过实例解析PHP数据类型转换方法
Jul 11 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
浅析SVN常见问题及解决方法
2013/06/21 PHP
PHP file_get_contents设置超时处理方法
2013/09/30 PHP
PHP函数addslashes和mysql_real_escape_string的区别
2014/04/22 PHP
PHP递归获取目录内所有文件的实现方法
2016/11/01 PHP
PHP+redis实现微博的推模型案例分析
2019/07/10 PHP
兼容Mozilla必须知道的知识。
2007/01/09 Javascript
摘自启点的main.js
2008/04/20 Javascript
Jquery从头学起第四讲 jquery入门教程
2010/08/01 Javascript
offsetHeight在OnLoad中获取为0的现象
2013/07/22 Javascript
Javascript 函数parseInt()转换时出现bug问题
2014/05/20 Javascript
Js+Jq获取URL参数的集中方法示例代码
2014/05/20 Javascript
通过location.replace禁止浏览器后退防止重复提交
2014/09/04 Javascript
js实现的Easy Tabs选项卡用法实例
2015/09/06 Javascript
JS转换HTML转义符的方法
2016/08/24 Javascript
JavaScript中Array的实用操作技巧分享
2016/09/11 Javascript
AngularJS使用自定义指令替代ng-repeat的方法
2016/09/17 Javascript
javascript prototype原型详解(比较基础)
2016/12/26 Javascript
jQuery Ajax实现跨域请求
2017/01/21 Javascript
非常漂亮的js烟花效果
2020/03/10 Javascript
Python使用自带的ConfigParser模块读写ini配置文件
2016/06/26 Python
Python爬虫DOTA排行榜爬取实例(分享)
2017/06/13 Python
python实现关键词提取的示例讲解
2018/04/28 Python
使用Python进行体育竞技分析(预测球队成绩)
2019/05/16 Python
python脚本后台执行方式
2019/12/21 Python
Python终端输出彩色字符方法详解
2020/02/11 Python
纯css3制作的火影忍者写轮眼开眼至轮回眼及进化过程实例
2014/11/11 HTML / CSS
nohup的用法
2014/08/10 面试题
关于逃课的检讨书
2014/01/23 职场文书
鼓励运动员的广播稿
2014/02/08 职场文书
股东合作协议书范本
2014/04/14 职场文书
人力资源管理专业应届生求职信
2014/04/24 职场文书
伏羲庙导游词
2015/02/09 职场文书
限期整改通知书
2015/04/22 职场文书
2015年学校少先队工作总结
2015/07/20 职场文书
2015团员个人年度总结
2015/11/24 职场文书
Nginx中使用Lua脚本与图片的缩略图处理的实现
2022/03/18 Servers