PHP中模拟链表和链表的基本操作示例
2016-02-27 00:00
871 查看
模拟链表:
链表的增删查改:
PHP 双链表(SplDoublyLinkedList)简介和使用实例
PHP小教程之实现双向链表
PHP小教程之实现链表
php实现单链表的实例代码
<?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 双链表(SplDoublyLinkedList)简介和使用实例
PHP小教程之实现双向链表
PHP小教程之实现链表
php实现单链表的实例代码
相关文章推荐
- OLAP、OLTP的介绍和比较
- Ubuntu Server Deploy 已经支持php7
- thinkphp使用js实现点击加载更多后, 底部无刷新加载出新的数据并显示在该页
- php 环境变量收集
- Yii2 RESTful Verbs
- PHP文件操作常用函数总结
- php的str_replace函数
- 大型Web应用运行时 PHP负载均衡指南
- Yii 1.x 到 Yii 2.x的细节变化
- PHP-003
- 如何把php中的数组导入到js中?------用json串做中间量吧
- php和js中创建数组的一个小区别
- PHP和MySQL的timestamp转换
- php从文本读入数据,处理结果再导入到文本
- PHP Laravel学习实践(一)最出色的php框架,让代码成为艺术
- PHP 表单验证 - 完成表单实例
- PHP 表单验证 - 验证 E-mail 和 URL
- PHP 表单验证 - 必填字段
- PHP 表单验证
- PHP 表单处理