PHP中模拟链表和链表的基本操作示例
2016-02-27 16:03
841 查看
模拟链表:
<?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); ?>
您可能感兴趣的文章:
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- [C/C++]反转链表
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- C#实现基于链表的内存记事本实例
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表