PHP单链表的实现代码


Posted in PHP onJuly 05, 2016

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。

单链表简介

链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。

关键代码如下所示:

<?php
/**
* 单链表
*/ 
class Demo
{
private $id;
public $name;
public $next;
public function __construct ($id = '', $name = '')
{
$this->id = $id;
$this->name = $name;
}
static public function show ($head)
{
$cur = $head;
while ($cur->next) {
echo $cur->next->id,'###',$cur->next->name,'<br />';
$cur = $cur->next;
}
echo '<hr />';
}
//尾插法
static public function push ($head, $node)
{
$cur = $head;
while (NULL != $cur->next) {
$cur = $cur->next;
}
$cur->next = $node;
return $head;
}
static public function insert($head, $node)
{
$cur = $head;
while (NULL != $cur->next) {
if ($cur->next->id > $node->id) {
break;
}
$cur = $cur->next;
}
$node->next = $cur->next;
$cur->next = $node;
return $head;
}
static public function edit($head, $node)
{
$cur = $head;
while (NULL != $cur->next) {
if ($cur->next->id == $node->id) {
break;
}
$cur = $cur->next;
}
$cur->next->name = $node->name;
return $head; 
}
static public function pop ($head, $node)
{
$cur = $head;
while (NULL != $cur->next) {
if ($cur->next == $node) {
break;
}
$cur = $cur->next;
}
$cur->next = $node->next;
return $head; 
}
}
$team = new Demo();
$node1 = new Demo(1, '唐三藏');
Demo::push($team, $node1);
$node1->name = '唐僧';
Demo::show($team);
// Demo::show($team);
$node2 = new Demo(2, '孙悟空');
Demo::insert($team, $node2);
// Demo::show($team);
$node3 = new Demo(5, '白龙马');
Demo::push($team, $node3);
// Demo::show($team);
$node4 = new Demo(3, '猪八戒');
Demo::insert($team, $node4);
// Demo::show($team);
$node5 = new Demo(4, '沙和尚');
Demo::insert($team, $node5);
// Demo::show($team);
$node4->name = '猪悟能';//php对象传引用,所以Demo::edit没有必要
// unset($node4);
// $node4 = new Demo(3, '猪悟能');
// Demo::edit($team, $node4);
Demo::pop($team, $node1);
Demo::show($team);

以上所述是小编给大家介绍的PHP单链表的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
我的论坛源代码(七)
Oct 09 PHP
PHP 开发环境配置(Zend Server安装)
Apr 28 PHP
PHP中error_reporting()函数的用法(修改PHP屏蔽错误)
Jul 01 PHP
必须收藏的23个php实用代码片段
Feb 02 PHP
php使用get_class_methods()函数获取分类的方法
Jul 20 PHP
基于Swoole实现PHP与websocket聊天室
Aug 03 PHP
php封装的单文件(图片)上传类完整实例
Oct 18 PHP
使用PHP免费发送定时短信的实例
Oct 24 PHP
简单谈谈PHP中的trait
Feb 25 PHP
PHP实现基于回溯法求解迷宫问题的方法详解
Aug 17 PHP
phpStudy配置多站点多域名方法及遇到的403错误解决方法
Oct 19 PHP
6个常见的 PHP 安全性攻击实例和阻止方法
Dec 16 PHP
php 数组字符串搜索array_search技巧
Jul 05 #PHP
php单链表实现代码分享
Jul 04 #PHP
Yii2.0预定义的别名功能小结
Jul 04 #PHP
Yii控制器中操作视图js的方法
Jul 04 #PHP
深入分析PHP优化及注意事项
Jul 04 #PHP
yum命令安装php7和相关扩展
Jul 04 #PHP
PHP中Array相关函数简介
Jul 03 #PHP
You might like
pw的一个放后门的方法分析
2007/10/08 PHP
php数组函数序列之array_slice() - 在数组中根据条件取出一段值,并返回
2011/11/07 PHP
浅析PHP 中move_uploaded_file 上传中文文件名失败
2019/04/17 PHP
php常用日期时间函数实例小结
2019/07/04 PHP
phpwind放自动注册方法
2006/12/02 Javascript
javascript 简练的几个函数
2009/08/29 Javascript
javascript中substr,substring,slice.splice的区别说明
2010/11/25 Javascript
基于jQuery的input输入框下拉提示层(自动邮箱后缀名)
2012/06/14 Javascript
node.js中使用socket.io制作命名空间
2014/12/15 Javascript
javascript中for/in循环及使用技巧
2015/09/01 Javascript
jquery表单验证插件formValidator使用方法
2016/04/01 Javascript
springMVC结合AjaxForm上传文件
2016/07/12 Javascript
原生js实现键盘控制div移动且解决停顿问题
2016/12/05 Javascript
原生JS下拉加载插件分享
2016/12/26 Javascript
jquery easyui dataGrid动态改变排序字段名的方法
2017/03/02 Javascript
微信小程序 仿猫眼实现实例代码
2017/03/14 Javascript
微信小程序动态显示项目倒计时效果
2017/06/13 Javascript
Javascript之图片的延迟加载的实例详解
2017/07/24 Javascript
jQuery中图片展示插件highslide.js的简单dom
2018/04/22 jQuery
vue获取form表单的值示例
2019/10/29 Javascript
Vue实现图片与文字混输效果
2019/12/04 Javascript
vue中使用v-for时为什么不能用index作为key
2020/04/04 Javascript
[01:58]2018DOTA2亚洲邀请赛趣味视频——交流
2018/04/03 DOTA
python安装numpy&amp;安装matplotlib&amp; scipy的教程
2017/11/02 Python
Python实现的对一个数进行因式分解操作示例
2019/06/27 Python
Python使用lambda表达式对字典排序操作示例
2019/07/25 Python
Python 使用多属性来进行排序
2019/09/01 Python
Keras中 ImageDataGenerator函数的参数用法
2020/07/03 Python
小学教师的自我评价范例
2013/10/31 职场文书
获奖的大学生创业计划书
2014/01/05 职场文书
资源工程专业毕业生求职信
2014/02/27 职场文书
《少年王冕》教学反思
2014/04/11 职场文书
党员查摆剖析材料
2014/10/10 职场文书
贷款承诺书
2015/01/20 职场文书
春秋淹城导游词
2015/02/11 职场文书
现场施工员岗位职责
2015/04/11 职场文书