PHP 实现单链表
来源:互联网 发布:巨人网络财务报表 编辑:程序博客网 时间:2024/05/29 14:42
<?php
header("Content-type:text/html;charset=utf-8");//链表节点
class node {
public static $count = -1; //节点id
public $name; //节点名称
public $next; //下一节点
public $id;
public function __construct($name) {
$this->id = self::$count;
$this->name = $name;
$this->next = null;
self::$count += 1;
}
}
//单链表
class singelLinkList {
private $header;
private $current;
public $count;
//构造方法
public function __construct($data = null) {
$this->header = new node ($data);
$this->current = $this->header;
$this->count = 0;
}
//添加节点数据
public function addLink($node) {
if($this->current->next != null)
$this->current = $this->current->next;
$this->count++;
$node->next = $this->current->next;
$this->current->next = $node;
}
//删除链表节点
public function delLink($id) {
$current = $this->header;
$flag = false;
while ( $current->next != null ) {
if ($current->next->id == $id) {
$flag = true;
break;
}
$current = $current->next;
}
if ($flag) {
$this->count--;
$current->next = $current->next->next;
} else {
echo "未找到id=" . $id . "的节点!<br>";
}
}
//获取链表
public function getLinkList() {
$this->checkNull();
$current = $this->header;
while ( $current->next != null ) {
echo 'id:' . $current->next->id . ' name:' . $current->next->name . "<br>";
if ($current->next->next == null) {
break;
}
$current = $current->next;
}
}
//获取长度
public function getLinkLength()
{
echo $this->count;
}
//获取当前节点
public function getCurrent()
{
$this->checkNull();
echo '当前节点id:' . $this->current->next->id . ' name:' . $this->current->next->name . "<br>";
}
//判断是否为空
public function checkNull()
{
if ($this->header->next == null) {
echo "链表为空!";
exit;
}
}
//获取节点名字
public function getLinkById($id) {
$this->checkNull();
$current = $this->header;
while ( $current->next != null ) {
if ($current->id == $id) {
break;
}
$current = $current->next;
}
echo '修改后id:' . $current->id . ' name:' . $current->name . "<br>";
}
//更新节点名称
public function updateLink($id, $name) {
$this->checkNull();
$current = $this->header;
while ( $current->next != null ) {
if ($current->id == $id) {
break;
}
$current = $current->next;
}
return $current->name = $name;
}
}
$list = new singelLinkList ();
$list->addLink ( new node ('aaaaaa' ) );
$list->addLink ( new node ('bbbbbb' ) );
$list->addLink ( new node ('cccccc' ) );
$list->addLink ( new node ('dddddd' ) );
echo "所有链表节点:<br/>";
$list->getLinkList();
echo "<br/>";
echo "当前链表末位节点:<br/>";
$list->getCurrent();
echo "<br/>";
echo "修改链表节点id为0的:<br/>";
$list->updateLink(0, '2222222');
echo "查找id为0的节点:<br/>";
$list->getLinkById(0);
echo "<br/>";
echo "删除链表节点id为0:<br/>";
$list->delLink(0);
echo "所有链表节点:<br/>";
$list->getLinkList();
echo "<br/>";
echo "所有链表节点:<br/>";
$list->getLinkLength ();
?>
0 0
- php 实现 单链表
- PHP 实现单链表
- php实现单链表
- php实现单链表操作
- PHP实现单链表
- 用PHP实现的单链表
- 用PHP实现的单链表
- 线性单链表的实现(php)
- php实现单链表的实例代码
- php实现单链表的基本操作
- php httpRequest(php实现httpRequest)
- 【php】 AES算法php实现
- PHP Array -- PHP 实现堆栈 PHP Stack
- PHP Array -- PHP实现队列 -- PHP Queues
- PHP实现文件下载
- PHP MVC架构实现
- php中实现多线程
- PHP中实现多线程
- vsftpd 配置
- C++ 对象的内存布局(上)
- 友好城市
- 【iOS】singleton的属性,以及getter在使用上的一点问题
- 【算法导论】决策树是满二叉树吗?
- PHP 实现单链表
- 动态生成WCF服务端的简单小例子
- 深入探讨 Java 类加载器(转)
- Eclipse Maven报错:Plugin execution not covered by lifecycle configuration
- hdu 1241 Oil Deposits
- 软件测试资料
- C语言中的字符串
- 1.总览--JUC框架
- linux下编译x264,生成lib库