PHP中模拟链表和链表的基本操作示例


Posted in PHP onFebruary 27, 2016

模拟链表:

<?php 
/**
 * PHP实现链表的基本操作
 */
class linkList {
  /**
   * 姓名
   * @var string
   */
  public $name = '';
   
  /**
   * 编号
   * @var int
   */
  public $id = 0;
   
  /*
   * 引用下一个对象
   */
  public $next = null;
   
  /**
   * 构造函数初始化数据
   * @param int $id
   * @param string $name
   */
  public function __construct($id = 0, $name = '') {
    $this->name = $name;
    $this->id  = $id;
  }
   
  /**
   * 遍历链表
   */
  public static function echo_link_list($head) {
    $curr = $head;
    while ($curr->next != null) {
      echo '姓名:'.$curr->next->name, ' 编号:'.$curr->next->id;
      echo '<br>';
      $curr = $curr->next;
    }
  }
   
  /**
   * 添加新节点
   */
  public static function add($head, $id, $name) {
    $curr = $head;
    $obj = new linkList($id, $name);
     
    while ($curr->next != null) {
      // 如果当前ID < 下一个ID,则添加到中间,添加节点到指定顺序位置
      if ($curr->next->id > $id) {
         
        $obj->next = $curr->next;
        $curr->next = $obj;
         
        return true;
      } else if ($curr->next->id == $id) {
         
        echo '当前Id:'.$id.'重复了,请不要继续添加了!';
        echo '<br>';
         
        return false;
      }
      $curr = $curr->next;
    }
    // 添加节点到尾部
    if ($curr->next == null) {
      $curr->next = $obj;
    }
  }
   
  /**
   * 删除节点
   */
  public static function del($head, $id) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next = $curr->next->next;
        return true;
      }
      $curr = $curr->next;
    }
  }
   
  /**
   * 修改节点
   */
  public static function edit($head, $id, $new_name) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next->name = $new_name;
      }
      $curr = $curr->next;
    }
  }
}
 
$head = new linkList();
linkList::add($head, 1, 'wangdk');
linkList::add($head, 2, 'sunshuzhen');
linkList::add($head, 8, 'wanghaha');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 3, 'wangdaye');
 
linkList::del($head, 1);
linkList::edit($head, 2, 'hahaha');
linkList::echo_link_list($head);
 
?>

链表的增删查改:

<?php 
/**
 * PHP实现链表的基本操作
 */
class linkList {
  /**
   * 姓名
   * @var string
   */
  public $name = '';
   
  /**
   * 编号
   * @var int
   */
  public $id = 0;
   
  /*
   * 引用下一个对象
   */
  public $next = null;
   
  /**
   * 构造函数初始化数据
   * @param int $id
   * @param string $name
   */
  public function __construct($id = 0, $name = '') {
    $this->name = $name;
    $this->id  = $id;
  }
   
  /**
   * 遍历链表
   */
  public static function echo_link_list($head) {
    $curr = $head;
    while ($curr->next != null) {
      echo '姓名:'.$curr->next->name, ' 编号:'.$curr->next->id;
      echo '<br>';
      $curr = $curr->next;
    }
  }
   
  /**
   * 添加新节点
   */
  public static function add($head, $id, $name) {
    $curr = $head;
    $obj = new linkList($id, $name);
     
    while ($curr->next != null) {
      // 如果当前ID < 下一个ID,则添加到中间,添加节点到指定顺序位置
      if ($curr->next->id > $id) {
         
        $obj->next = $curr->next;
        $curr->next = $obj;
         
        return true;
      } else if ($curr->next->id == $id) {
         
        echo '当前Id:'.$id.'重复了,请不要继续添加了!';
        echo '<br>';
         
        return false;
      }
      $curr = $curr->next;
    }
    // 添加节点到尾部
    if ($curr->next == null) {
      $curr->next = $obj;
    }
  }
   
  /**
   * 删除节点
   */
  public static function del($head, $id) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next = $curr->next->next;
        return true;
      }
      $curr = $curr->next;
    }
  }
   
  /**
   * 修改节点
   */
  public static function edit($head, $id, $new_name) {
    $curr = $head;
     
    while($curr->next != null) {
      if ($curr->next->id == $id) {
        $curr->next->name = $new_name;
      }
      $curr = $curr->next;
    }
  }
}
 
$head = new linkList();
linkList::add($head, 1, 'wangdk');
linkList::add($head, 2, 'sunshuzhen');
linkList::add($head, 8, 'wanghaha');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 6, 'wangchufen');
linkList::add($head, 3, 'wangdaye');
 
linkList::del($head, 1);
linkList::edit($head, 2, 'hahaha');
linkList::echo_link_list($head);
 
?>
PHP 相关文章推荐
BBS(php &amp; mysql)完整版(一)
Oct 09 PHP
织梦模板标记简介
Mar 11 PHP
详谈PHP文件目录基础操作
Nov 11 PHP
php实现统计网站在线人数的方法
May 12 PHP
全面解读PHP的Yii框架中的日志功能
Mar 17 PHP
php基于jquery的ajax技术传递json数据简单实例
Apr 15 PHP
php使用文本统计访问量的方法
May 12 PHP
CodeIgniter生成静态页的方法
May 17 PHP
TP3.2批量上传文件或图片 同名冲突问题的解决方法
Aug 01 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
Aug 31 PHP
PHP7新功能总结
Apr 14 PHP
php array 转json及java 转换 json数据格式操作示例
Nov 13 PHP
PHP使用Memcache时模拟命名空间及缓存失效问题的解决
Feb 27 #PHP
简单谈谈PHP中strlen 函数
Feb 27 #PHP
详解PHP的Laravel框架中Eloquent对象关系映射使用
Feb 26 #PHP
PHP文件缓存smarty模板应用实例分析
Feb 26 #PHP
PHP计算当前坐标3公里内4个角落的最大最小经纬度实例
Feb 26 #PHP
PHP实现根据时间戳获取周几的方法
Feb 26 #PHP
PHP将二维数组某一个字段相同的数组合并起来的方法
Feb 26 #PHP
You might like
php根据操作系统转换文件名大小写的方法
2014/02/24 PHP
Laravel 5 框架入门(二)构建 Pages 的管理功能
2015/04/09 PHP
jquery 表单进行客户端验证demo
2009/08/24 Javascript
script的async属性以非阻塞的模式加载脚本
2013/01/15 Javascript
jQuery当鼠标悬停时放大图片的效果实例
2013/07/03 Javascript
8个实用的jQuery技巧
2014/03/04 Javascript
js实现select跳转功能代码
2014/10/22 Javascript
纯javascript响应式树形菜单效果
2015/11/10 Javascript
利用jquery制作滚动到指定位置触发动画
2016/03/26 Javascript
深入解析Backbone.js框架的依赖库Underscore.js的作用
2016/05/07 Javascript
jQuery实现的多张图无缝滚动效果【测试可用】
2016/09/12 Javascript
jQuery展示表格点击变色、全选、删除
2017/01/05 Javascript
vue 2.0路由之路由嵌套示例详解
2017/05/08 Javascript
Bootstrap 树控件使用经验分享(图文解说)
2017/11/06 Javascript
JavaScript实现创建自定义对象的常用方式总结
2018/07/09 Javascript
JS实现吸顶特效
2020/01/08 Javascript
基于vue.js仿淘宝收货地址并设置默认地址的案例分析
2020/08/20 Javascript
react+antd 递归实现树状目录操作
2020/11/02 Javascript
[50:05]VGJ.S vs OG 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
python中使用xlrd、xlwt操作excel表格详解
2015/01/29 Python
发布你的Python模块详解
2016/09/15 Python
python判断字符串或者集合是否为空的实例
2019/01/23 Python
python中用logging实现日志滚动和过期日志删除功能
2019/08/20 Python
python GUI库图形界面开发之PyQt5信号与槽基本操作
2020/02/25 Python
HTML5 input placeholder 颜色修改示例
2014/05/30 HTML / CSS
台湾网友喜爱的综合型网路购物商城:Yahoo! 奇摩购物中心
2018/03/10 全球购物
酒吧员工的岗位职责
2013/11/26 职场文书
竞选演讲稿范文
2013/12/28 职场文书
政法大学毕业生自荐信范文
2014/01/01 职场文书
房地产营销策划方案
2014/02/08 职场文书
婚前协议书范本两则
2014/10/16 职场文书
工作保证书
2015/01/17 职场文书
php微信小程序解包过程实例详解
2021/03/31 PHP
利用Python+OpenCV三步去除水印
2021/05/28 Python
只用Python就可以制作的简单词云
2021/06/07 Python
浅谈GO中的Channel以及死锁的造成
2022/03/18 Golang