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 相关文章推荐
以文本方式上传二进制文件的PHP程序
Oct 09 PHP
PHP 截取字符串 分别适合GB2312和UTF8编码情况
Feb 12 PHP
PHP中date()日期函数有关参数整理
Jul 19 PHP
基于php实现长连接的方法与注意事项的问题
May 10 PHP
浅析ThinkPHP中execute和query方法的区别
Jun 13 PHP
php传值赋值和传地址赋值用法实例分析
Jun 20 PHP
php算法实例分享
Jul 14 PHP
学习php设计模式 php实现装饰器模式(decorator)
Dec 07 PHP
Yii框架数据模型的验证规则rules()被执行的方法
Dec 02 PHP
php die()与exit()的区别实例详解
Dec 03 PHP
PHP实现基于栈的后缀表达式求值功能
Nov 10 PHP
php 读写json文件及修改json的方法
Mar 07 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使用wordwrap格式化文本段落的方法
2015/03/17 PHP
在CentOS系统上从零开始搭建WordPress博客的全流程记录
2016/04/21 PHP
php简单的上传类分享
2016/05/15 PHP
PHP chr()函数讲解
2019/02/11 PHP
Aster vs Newbee BO5 第一场2.19
2021/03/10 DOTA
用jquery实现下拉菜单效果的代码
2010/07/25 Javascript
使用mini-define实现前端代码的模块化管理
2014/12/25 Javascript
举例简介AngularJS的内部语言环境
2015/06/17 Javascript
JavaScript中的定时器之Item23的合理使用
2015/10/30 Javascript
浏览器环境下JavaScript脚本加载与执行探析之defer与async特性
2016/01/14 Javascript
web 前端常用组件之Layer弹出层组件
2016/09/22 Javascript
ES6新特性之模块Module用法详解
2017/04/01 Javascript
js实现倒计时关键代码
2017/05/05 Javascript
vue将对象新增的属性添加到检测序列的方法
2018/02/24 Javascript
jQuery实现下拉菜单动态添加数据点击滑出收起其他功能
2018/06/14 jQuery
vue-cli2打包前和打包后的css前缀不一致的问题解决
2018/08/24 Javascript
vue 实现动态路由的方法
2020/07/06 Javascript
[00:35]DOTA2上海特级锦标赛 EG战队宣传片
2016/03/04 DOTA
python创建线程示例
2014/05/06 Python
Python 中的 else详解
2016/04/23 Python
Python爬虫包 BeautifulSoup  递归抓取实例详解
2017/01/28 Python
libreoffice python 操作word及excel文档的方法
2019/07/04 Python
在Python IDLE 下调用anaconda中的库教程
2020/03/09 Python
Python图片处理模块PIL操作方法(pillow)
2020/04/07 Python
详细分析Python垃圾回收机制
2020/07/01 Python
基于Python实现简单学生管理系统
2020/07/24 Python
Python文件名匹配与文件复制的实现
2020/12/11 Python
中国高端鲜花第一品牌:roseonly(一生只送一人)
2017/02/12 全球购物
竞选体育委员演讲稿
2014/04/26 职场文书
2014年教师思想工作总结
2014/12/03 职场文书
初中英语教师个人工作总结
2015/02/09 职场文书
大学生暑期实践报告
2015/07/13 职场文书
银行岗位培训心得体会
2016/01/09 职场文书
《这片土地是神圣的》教学反思
2016/02/16 职场文书
2019年度开业庆典祝福语大全!
2019/07/05 职场文书
CSS+HTML 实现顶部导航栏功能
2021/08/30 HTML / CSS