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实现的获取URL信息的类
Jan 02 PHP
PHP 遍历XP文件夹下所有文件
Nov 27 PHP
通过PHP CLI实现简单的数据库实时监控调度
Jul 01 PHP
Ext.data.PagingMemoryProxy分页一次性读取数据的实现代码
Apr 07 PHP
PHP+jQuery 注册模块的改进(一):验证码存入SESSION
Oct 14 PHP
PHP中Header使用的HTTP协议及常用方法小结
Nov 04 PHP
php使用GD创建保持宽高比缩略图的方法
Apr 17 PHP
php实现二进制和文本相互转换的方法
Apr 18 PHP
ThinkPHP控制器详解
Jul 27 PHP
YII动态模型(动态表名)支持分析
Mar 29 PHP
CI框架整合widget(页面格局)的方法
May 17 PHP
php设计模式之组合模式实例详解【星际争霸游戏案例】
Mar 27 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 图片上传代码
2011/09/13 PHP
PHP使用PDO连接ACCESS数据库
2015/03/05 PHP
php正则修正符用法实例详解
2016/12/29 PHP
php无限极分类实现方法分析
2019/07/04 PHP
extjs 列表框(multiselect)的动态添加列表项的方法
2009/07/31 Javascript
使用js获取QueryString的方法小结
2010/02/28 Javascript
return false;和e.preventDefault();的区别
2010/07/11 Javascript
javascript中简单的进制转换代码实例
2013/10/26 Javascript
浅析XMLHttpRequest的缓存问题
2013/12/13 Javascript
jQuery中:not选择器用法实例
2014/12/30 Javascript
JavaScript通过字典进行字符串翻译转换的方法
2015/03/19 Javascript
Django1.7+JQuery+Ajax验证用户注册集成小例子
2017/04/08 jQuery
详解如何去除vue项目中的#——History模式
2017/10/13 Javascript
vue.js默认路由不加载linkActiveClass问题的解决方法
2017/12/11 Javascript
echarts鼠标覆盖高亮显示节点及关系名称详解
2018/03/17 Javascript
Nuxt项目支持eslint+pritter+typescript的实现
2019/05/20 Javascript
JavaScript字符串转数字的简单实现方法
2020/11/27 Javascript
vue集成一个支持图片缩放拖拽的富文本编辑器
2021/01/29 Vue.js
Python socket C/S结构的聊天室应用实现
2014/11/30 Python
替换python字典中的key值方法
2018/07/06 Python
深入浅析Python获取对象信息的函数type()、isinstance()、dir()
2018/09/17 Python
python requests爬取高德地图数据的实例
2018/11/10 Python
Pycharm中安装Pygal并使用Pygal模拟掷骰子(推荐)
2020/04/08 Python
使用PyQt5实现图片查看器的示例代码
2020/04/21 Python
Android本地应用打开方法——通过html5写连接
2016/03/11 HTML / CSS
小天鹅官方商城:LittleSwan
2017/06/16 全球购物
英国婴儿和儿童服装网站:Vertbaudet
2018/04/02 全球购物
德国在线香料制造商:Gewürzland
2020/03/10 全球购物
三个Unix的命令面试题
2015/04/12 面试题
高二学生评语大全
2014/04/25 职场文书
学生会干部自我鉴定2014
2014/09/18 职场文书
领导班子四风对照检查材料思想汇报
2014/09/26 职场文书
小学感恩节活动总结
2015/03/24 职场文书
汽车4S店前台接待岗位职责
2015/04/03 职场文书
2016年小学“我们的节日·中秋节”活动总结
2016/04/05 职场文书
MySQL数据库实验之 触发器和存储过程
2022/06/21 MySQL