温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php中链表的详细介绍

发布时间:2021-08-04 17:20:32 来源:亿速云 阅读:109 作者:chen 栏目:web开发

这篇文章主要介绍“php中链表的详细介绍”,在日常操作中,相信很多人在php中链表的详细介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php中链表的详细介绍”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

链表:是由一系列节点构成的数据结构。

每个节点包含两个部分:一个是存储数据的数据域;一个是存储下一个节点的地址的指针域。

以下是本人用PHP实现的一个简单的链表案例,仅供学习,请多多指教。

<?php

/*

节点类

**/

class listnode

{

public $id;//节点ID

public $name;//节点名称

public $next;//下一个节点

//节点构造函数

public function __construct($id,$name)

{

$this->id=$id;

$this->name=$name;

$this->next=null;

}

}

/*

链表类

**/

class linklist

{

private $header;

//链表构造函数

public function __construct($id=null,$name=null)

{

$this->header=new listnode($id,$name);

}

//增加节点

public function add_list_node($id,$name)

{

$node=new listnode($id,$name);

$tmp=$this->header;

$exists=false;

while($tmp->next!==null)

{

if($tmp->next->id == $node->id)

{

$exists=true;

break;

}

if($tmp->next->id > $node->id )

{

break;

}

$tmp=$tmp->next;

}

if(!$exists)

{

$node->next=$tmp->next;

$tmp->next=$node;

}

else

{

echo '节点ID不能中复';

}

}

//输出链表

public function display()

{

$tmp=$this->header;

if($tmp->next===null)

{

echo '链表为空!';

return ;

}

while($tmp->next!==null)

{

echo 'id:'.$tmp->next->id.'  name:'.$tmp->next->name.'-->';

$tmp=$tmp->next;

}

}

//查找节点

public function find($id)

{

$tmp=$this->header;

if($tmp->next===null)

{

echo '链表为空,没有找到节点<br/>';

}

while($tmp->next!==null)

{

if($tmp->next->id=$id)

{

return $tmp->next;

}

$tmp=$tmp->next;

}

}

//删除节点

public function delnode($id)

{

$tmp=$this->header;

if($tmp->next===null)

{

echo '链表为空!<br/>';

return ;

}

while($tmp->next!==null)

{

if($tmp->next->id==$id)

{

$tmp->next=$tmp->next->next;

break;

}

$tmp=$tmp->next;

}

}

//更新节点NAME

public function updatenode($id,$name)

{

$tmp=$this->header;

if($tmp->next===null)

{

$node=new listnode($id,$name);

$node->next=$tmp->next;

$tmp->next=$node;

}

$flag=false;

while($tmp->next!==null)

{

if($tmp->next->id==$id)

{

$tmp->next->name=$name;

$flag=true;

break;

}

$tmp=$tmp->next;

}

if(!$flag)

{

$node=new listnode($id,$name);

$node->next=$tmp->next;

$tmp->next=$node;

}

}

}

header('content-type:text/html;charset=utf-8');

$linklist=new linklist();

$linklist->display();

echo '<br/>';

$linklist->updatenode(9,'节点9');

echo '<br/>';

$linklist->display();

echo '<br/>';

$linklist->add_list_node(1,'节点1');

$linklist->display();

echo '<br/>';

$linklist->add_list_node(2,'节点2');

$linklist->add_list_node(3,'节点3');

$linklist->add_list_node(4,'节点4');

$linklist->add_list_node(5,'节点5');

$linklist->add_list_node(6,'节点6');

$linklist->display();

$linkl=$linklist->find(1);

echo '<br/>';

echo $linkl->id.'  name:'.$linkl->name;

$linklist->delnode(3);

echo '<br/>';

$linklist->display();

echo '<br/>';

$linklist->add_list_node(3,'节点3');

echo '<br/>';

$linklist->display();

$linklist->add_list_node(8,'节点8');

echo '<br/>';

$linklist->display();

$linklist->add_list_node(7,'节点7');

echo '<br/>';

$linklist->display();

$linklist->delnode(8);

echo '<br/>';

$linklist->display();

echo '<br/>';

$linklist->updatenode(9,'节点9');

echo '<br/>';

$linklist->display();

echo '<br/>';

?>

到此,关于“php中链表的详细介绍”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI