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 相关文章推荐
php tp验证表单与自动填充函数代码
Feb 22 PHP
PHP中操作ini配置文件的方法
Apr 25 PHP
php的数组与字符串的转换函数整理汇总
Jul 18 PHP
多个PHP中文字符串截取函数
Nov 12 PHP
浅谈Eclipse PDT调试PHP程序
Jun 09 PHP
编写PHP脚本使WordPress的主题支持Widget侧边栏
Dec 14 PHP
PDO的安全处理与事物处理方法
Oct 31 PHP
php array_walk 对数组中的每个元素应用用户自定义函数详解
Nov 18 PHP
php接口技术实例详解
Dec 07 PHP
php 反斜杠处理函数addslashes()和stripslashes()实例详解
Dec 25 PHP
PHP实现的简单排列组合算法应用示例
Jun 20 PHP
laravel 解决groupBy时出现的错误 isn't in Group By问题
Oct 17 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
Apache连接PHP后无法启动问题解决思路
2015/06/18 PHP
Yii中srbac权限扩展模块工作原理与用法分析
2016/07/14 PHP
css动画效果之animation的常用样式
2021/03/09 HTML / CSS
js confirm()方法的使用方法实例
2013/07/13 Javascript
ie与ff下的event事件使用介绍
2013/11/25 Javascript
jQuery层动画定位滑动效果的方法
2015/04/30 Javascript
JSON 必知必会 观后记
2016/10/27 Javascript
Vue 仿百度搜索功能实现代码
2017/02/16 Javascript
微信小程序开发之数据存储 参数传递 数据缓存
2017/04/13 Javascript
Vue.js实例方法之生命周期详解
2017/07/03 Javascript
基于脚手架创建Vue项目实现步骤详解
2020/08/03 Javascript
[47:02]2018DOTA2亚洲邀请赛3月29日 小组赛B组 VP VS paiN
2018/03/30 DOTA
在Python的gevent框架下执行异步的Solr查询的教程
2015/04/16 Python
python创建临时文件夹的方法
2015/07/06 Python
使用Python对SQLite数据库操作
2017/04/06 Python
Python实现通讯录功能
2018/02/22 Python
Python中max函数用于二维列表的实例
2018/04/03 Python
pandas数值计算与排序方法
2018/04/12 Python
Python实现的计算器功能示例
2018/04/26 Python
python3实现网络爬虫之BeautifulSoup使用详解
2018/12/19 Python
Python按钮的响应事件详解
2019/03/04 Python
Python 异步协程函数原理及实例详解
2019/11/13 Python
浅谈SciPy中的optimize.minimize实现受限优化问题
2020/02/29 Python
Python 读取xml数据,cv2裁剪图片实例
2020/03/10 Python
sublime3之内网安装python插件Anaconda的流程
2020/11/10 Python
Canvas波浪花环的示例代码
2020/08/21 HTML / CSS
MCAKE蛋糕官方网站:一直都是巴黎的味道
2018/02/06 全球购物
美国婴儿服装购物网站:Gerber Childrenswear
2020/05/06 全球购物
中专生自荐信
2013/10/12 职场文书
教师师德演讲稿
2014/05/06 职场文书
违反单位工作制度检讨书
2014/10/25 职场文书
教师学期末个人总结
2015/02/13 职场文书
2015年简历自我评价范文
2015/03/11 职场文书
冲出亚马逊观后感
2015/06/03 职场文书
Python list去重且保持原顺序不变的方法
2021/04/03 Python
关于pytest结合csv模块实现csv格式的数据驱动问题
2022/05/30 Python