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 相关文章推荐
php更新mysql后获取影响的行数发生异常解决方法
Mar 28 PHP
PHP中HTML标签过滤技巧
Jan 07 PHP
简单的php中文转拼音的实现代码
Feb 11 PHP
ThinkPHP3.1的Widget新用法
Jun 19 PHP
ThinkPHP模板中数组循环实例
Oct 30 PHP
php中smarty模板条件判断用法实例
Jun 11 PHP
php 防止表单重复提交两种实现方法
Nov 03 PHP
Zend Framework入门教程之Zend_Db数据库操作详解
Dec 08 PHP
PHP生成唯一ID之SnowFlake算法
Dec 17 PHP
php实现遍历文件夹的方法汇总
Mar 02 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
Apr 27 PHP
如何通过Apache在本地配置多个虚拟主机
Jul 29 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模板,主要想体现一下思路
2006/12/25 PHP
PHP屏蔽过滤指定关键字的方法
2014/11/03 PHP
JavaScript实现滚动栏效果的方法
2015/04/27 PHP
使用PHP+AJAX让WordPress动态加载文章的教程
2015/12/11 PHP
PHP实现多级分类生成树的方法示例
2017/02/07 PHP
Paypal实现循环扣款(订阅)功能
2017/03/23 PHP
利用ASP发送和接收XML数据的处理方法与代码
2007/11/13 Javascript
ASP.NET jQuery 实例7 通过jQuery来获取DropDownList的Text/Value属性值
2012/02/03 Javascript
Javascript跨域请求的4种解决方式
2013/03/17 Javascript
Node.js中的模块机制学习笔记
2014/11/04 Javascript
JavaScript如何调试有哪些建议和技巧附五款有用的调试工具
2015/10/28 Javascript
浅析jQuery Ajax请求参数和返回数据的处理
2016/02/24 Javascript
JS判断浏览器是否安装flash插件的简单方法
2016/09/13 Javascript
详解单页面路由工程使用微信分享及二次分享解决方案
2019/02/22 Javascript
countUp.js实现数字动态变化效果
2019/10/17 Javascript
Python设计模式之代理模式实例
2014/04/26 Python
跟老齐学Python之有容乃大的list(3)
2014/09/15 Python
在Python中使用base64模块处理字符编码的教程
2015/04/28 Python
python搭建虚拟环境的步骤详解
2016/09/27 Python
利用Python生成文件md5校验值函数的方法
2017/01/10 Python
浅谈django三种缓存模式的使用及注意点
2018/09/30 Python
python2爬取百度贴吧指定关键字和图片代码实例
2019/08/14 Python
PyCharm2018 安装及破解方法实现步骤
2019/09/09 Python
ubuntu 18.04 安装opencv3.4.5的教程(图解)
2019/11/04 Python
Python虚拟环境virtualenv创建及使用过程图解
2020/12/08 Python
中文系师范生自荐信
2013/10/01 职场文书
安全标准化实施方案
2014/02/20 职场文书
企业员工薪酬方案
2014/06/04 职场文书
关于安全的标语
2014/06/10 职场文书
大学生党员批评与自我批评
2014/09/28 职场文书
总经理岗位职责
2015/02/04 职场文书
2015年招商引资工作总结
2015/04/25 职场文书
繁星春水读书笔记
2015/06/30 职场文书
2016学习依法治国心得体会
2016/01/15 职场文书
AJAX实现省市县三级联动效果
2021/10/16 Javascript
基于Python实现对比Exce的工具
2022/04/07 Python