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 相关文章推荐
自己前几天写的无限分类类
Feb 14 PHP
供参考的 php 学习提高路线分享
Oct 23 PHP
php导出word文档与excel电子表格的简单示例代码
Mar 08 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(九)
Jun 24 PHP
PHP获取时间排除周六、周日的两个方法
Jun 30 PHP
PHP实现把文本中的URL转换为链接的auolink()函数分享
Jul 29 PHP
thinkphp中memcache的用法实例
Nov 29 PHP
PHP中curl_setopt函数用法实例分析
Apr 16 PHP
PHP中通过trigger_error触发PHP错误示例
Jun 23 PHP
PHP+Redis 消息队列 实现高并发下注册人数统计的实例
Jan 29 PHP
PHP Include文件实例讲解
Feb 15 PHP
PHP使用ajax的post方式下载excel文件简单示例
Aug 06 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程序的php代码
2008/04/07 PHP
关于php操作mysql执行数据库查询的一些常用操作汇总
2013/06/24 PHP
php+ajax+json 详解及实例代码
2016/12/12 PHP
laravel框架中路由设置,路由参数和路由命名实例分析
2019/11/23 PHP
使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
2010/05/07 Javascript
JS小游戏之宇宙战机源码详解
2014/09/25 Javascript
Javascript实现通过选择周数显示开始日和结束日的实现代码
2016/05/30 Javascript
分享jQuery网页元素拖拽插件
2020/12/01 Javascript
BootStrap表单验证实例代码
2017/01/13 Javascript
jQuery中layer分页器的使用
2017/03/13 Javascript
vue.js实现备忘录功能的方法
2017/07/10 Javascript
vue axios用法教程详解
2017/07/23 Javascript
微信小程序上传图片实例
2018/05/28 Javascript
layui下拉列表select实现可输入查找的方法
2019/09/28 Javascript
javascript sort()对数组中的元素进行排序详解
2019/10/13 Javascript
jQuery鼠标滑过横向时间轴样式(代码详解)
2019/11/01 jQuery
vue 解决移动端弹出键盘导致页面fixed布局错乱的问题
2019/11/06 Javascript
[00:32]2018DOTA2亚洲邀请赛Secret出场
2018/04/03 DOTA
跨平台python异步回调机制实现和使用方法
2013/11/26 Python
Python通过OpenCV的findContours获取轮廓并切割实例
2018/01/05 Python
破解安装Pycharm的方法
2018/10/19 Python
使用python模拟高斯分布例子
2019/12/09 Python
使用PyTorch实现MNIST手写体识别代码
2020/01/18 Python
20行Python代码实现视频字符化功能
2020/04/13 Python
Django与AJAX实现网页动态数据显示的示例代码
2021/02/24 Python
跑步爱好者一站式服务网站:Jack Rabbit
2016/09/01 全球购物
英国时尚服饰电商:Boohoo
2017/10/12 全球购物
中国旅游网站:途牛旅游网
2019/09/29 全球购物
Linux中如何用命令创建目录
2015/01/12 面试题
为什么要使用servlet
2016/01/17 面试题
实习医生自我评价
2013/09/22 职场文书
民族学专业求职信
2014/07/28 职场文书
党员国庆节演讲稿范文2014
2014/09/21 职场文书
深入浅析Django MTV模式
2021/09/04 Python
MSSQL基本语法操作
2022/04/11 SQL Server
Golang Elasticsearches 批量修改查询及发送MQ
2022/04/19 Golang