php单链表实现代码分享


Posted in PHP onJuly 04, 2016

本文实例为大家分享了php单链表的具体代码,供大家参考,具体内容如下

<?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 相关文章推荐
JAVA/JSP学习系列之四
Oct 09 PHP
PHP+AJAX实现无刷新注册(带用户名实时检测)
Dec 02 PHP
php实现rc4加密算法代码
Apr 25 PHP
php中判断文件空目录是否有读写权限的函数代码
Aug 07 PHP
php简单开启gzip压缩方法(zlib.output_compression)
Apr 13 PHP
基于PHP常用字符串的总结(待续)
Jun 07 PHP
PHP多线程之内部多线程实例分析
Mar 09 PHP
php关键字仅替换一次的实现函数
Oct 29 PHP
php验证身份证号码正确性的函数
Jul 20 PHP
PHP让数组中有相同值的组成新的数组实例
Dec 31 PHP
php连接MSsql server的五种方法总结
Mar 04 PHP
php正则表达式使用方法整理集合
Jan 31 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
PHP与Java对比学习日期时间函数
Jul 03 #PHP
Windows Server 2008 R2和2012中PHP连接MySQL过慢的解决方法
Jul 02 #PHP
You might like
发款php蜘蛛统计插件只要有mysql就可用
2010/10/12 PHP
php编程实现获取excel文档内容的代码实例
2011/06/28 PHP
php基础设计模式大全(注册树模式、工厂模式、单列模式)
2015/08/31 PHP
php加密之discuz内容经典加密方式实例详解
2017/02/04 PHP
PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法
2017/11/10 PHP
laravel在中间件内生成参数并且传递到控制器中的2种姿势
2019/10/15 PHP
IE8 下的Js错误HTML Parsing Error...
2009/08/14 Javascript
Ajax请求在数据量大的时候出现超时的解决方法
2014/02/27 Javascript
jQuery中replaceWith()方法用法实例
2014/12/25 Javascript
深入学习nodejs中的async模块的使用方法
2017/07/12 NodeJs
使用jQuery实现简单的tab框实例
2017/08/22 jQuery
vue-cli 3.x 配置Axios(proxyTable)跨域代理方法
2018/09/19 Javascript
vue-drag-chart 拖动/缩放图表组件的实例代码
2020/04/10 Javascript
vue 路由缓存 路由嵌套 路由守卫 监听物理返回操作
2020/08/06 Javascript
基于JS实现快速读取TXT文件
2020/08/25 Javascript
解析Python中的变量、引用、拷贝和作用域的问题
2015/04/07 Python
Python输出汉字字库及将文字转换为图片的方法
2016/06/04 Python
Django中针对基于类的视图添加csrf_exempt实例代码
2018/02/11 Python
python如何在循环引用中管理内存
2018/03/20 Python
将Django项目部署到CentOs服务器中
2018/10/18 Python
Python+OpenCV感兴趣区域ROI提取方法
2019/01/10 Python
对python中类的继承与方法重写介绍
2019/01/20 Python
对Python w和w+权限的区别详解
2019/01/23 Python
django 实现将本地图片存入数据库,并能显示在web上的示例
2019/08/07 Python
解决pycharm中导入自己写的.py函数出错问题
2020/02/12 Python
Python numpy矩阵处理运算工具用法汇总
2020/07/13 Python
HTML5 本地存储实现购物车功能
2017/09/07 HTML / CSS
HTML5制作表格样式
2016/11/15 HTML / CSS
经典大学生求职信范文
2014/01/06 职场文书
四风问题自查报告剖析材料
2014/02/08 职场文书
党建工作整改措施
2014/10/28 职场文书
2014年档案室工作总结
2014/12/01 职场文书
春季运动会开幕词
2015/01/28 职场文书
告知书格式
2015/07/01 职场文书
导游词之淮安明祖陵
2019/11/25 职场文书
Vue+Element UI实现概要小弹窗的全过程
2021/05/30 Vue.js