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中养成7个面向对象的好习惯
Jul 17 PHP
怎样使用php与jquery设置和读取cookies
Aug 08 PHP
php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
Sep 28 PHP
PHP6 中可能会出现的新特性预览
Apr 04 PHP
php数组生成html下拉列表的方法
Jul 20 PHP
关于PHP 如何用 curl 读取 HTTP chunked 数据
Feb 26 PHP
PHP随机数 C扩展随机数
May 04 PHP
PHP扩展框架之Yaf框架的安装与使用
May 18 PHP
PHP magento后台无法登录问题解决方法
Nov 24 PHP
关于ThinkPhp 框架表单验证及ajax验证问题
Jul 19 PHP
windows环境下使用Composer安装ThinkPHP5
May 18 PHP
PHP基于timestamp和nonce实现的防止重放攻击方案分析
Jul 26 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
写一个用户在线显示的程序
2006/10/09 PHP
如何用PHP实现插入排序?
2013/04/10 PHP
Windows下的PHP 5.3.x安装 Zend Guard Loader教程
2014/09/06 PHP
Yii框架函数简单用法分析
2019/09/09 PHP
Jquery 滑入滑出效果实现代码
2010/03/27 Javascript
JS父页面与子页面相互传值方法
2014/03/05 Javascript
Extjs 4.x 得到form CheckBox 复选框的值
2014/05/04 Javascript
JavaScript中的setUTCDate()方法使用详解
2015/06/11 Javascript
详解javascript中的事件处理
2015/11/06 Javascript
BootstrapTable refresh 方法使用实例简单介绍
2017/02/20 Javascript
正则验证小数点后面只能有两位数的方法
2017/02/28 Javascript
Angular.js中下拉框实现渲染html的方法
2017/06/18 Javascript
vue element-ui table表格滚动加载方法
2018/03/02 Javascript
用Angular实现一个扫雷的游戏示例
2020/05/15 Javascript
[01:00:04]DOTA2上海特级锦标赛B组小组赛#1 Alliance VS Spirit第二局
2016/02/26 DOTA
python网络编程之读取网站根目录实例
2014/09/30 Python
浅谈python中的getattr函数 hasattr函数
2016/06/14 Python
Python中read()、readline()和readlines()三者间的区别和用法
2017/07/30 Python
python2.7实现邮件发送功能
2018/12/12 Python
opencv python统计及绘制直方图的方法
2019/01/21 Python
Pandas之ReIndex重新索引的实现
2019/06/25 Python
python中append实例用法总结
2019/07/30 Python
Pytorch之保存读取模型实例
2019/12/30 Python
Pytorch 实现冻结指定卷积层的参数
2020/01/06 Python
python使用pandas抽样训练数据中某个类别实例
2020/02/28 Python
深入浅析python 中的self和cls的区别
2020/06/20 Python
Python如何使用vars返回对象的属性列表
2020/10/17 Python
详解Scrapy Redis入门实战
2020/11/18 Python
香港卓悦化妆品官网:BONJOUR
2017/09/21 全球购物
文员自我评价怎么写
2013/09/19 职场文书
体育教师自荐信范文
2013/12/16 职场文书
手工社团活动方案
2014/02/17 职场文书
保护环境的标语
2014/06/09 职场文书
委托公证书格式
2015/01/26 职场文书
布达拉宫导游词
2015/02/02 职场文书
职工趣味运动会开幕词
2016/03/04 职场文书