本文实例讲述了php链表用法。分享给大家供大家参考。具体如下:
这里简单介绍了php链表的基本用法,包括链表节点的创建、遍历、更新等操作。
<?php
/**
* @author mzxy
* @copyright 2011
* @param php链表
*/
/**
*
*节点类
*/
class node
{
private $data;//节点数据
private $next;//下一节点
public function setdata($value){
$this->data=$value;
}
public function setnext($value){
$this->next=$value;
}
public function getdata(){
return $this->data;
}
public function getnext(){
return $this->next;
}
public function __construct($data,$next){
$this->setdata($data);
$this->setnext($next);
}
}//功能类
class linklist
{
private $header;//头节点
private $size;//长度
public function getsize(){
$i=0;
$node=$this->header;
while($node->getnext()!=null)
{ $i++;
$node=$node->getnext();
}
return $i;
}
public function setheader($value){
$this->header=$value;
}
public function getheader(){
return $this->header;
}
public function __construct(){
header("content-type:text/html; charset=utf-8");
$this->setheader(new node(null,null));
}
/**
*@author mzxy
*@param $data--要添加节点的数据
*
*/
public function add($data)
{
$node=$this->header;
while($node->getnext()!=null)
{
$node=$node->getnext();
}
$node->setnext(new node($data,null));
}
/**
*@author mzxy
*@param $data--要移除节点的数据
*
*/
public function removeat($data)
{
$node=$this->header;
while($node->getdata()!=$data)
{
$node=$node->getnext();
}
$node->setnext($node->getnext());
$node->setdata($node->getnext()->getdata());
}
/**
*@author mzxy
*@param 遍历
*
*/
public function get()
{
$node=$this->header;
if($node->getnext()==null){
print("数据集为空!");
return;
}
while($node->getnext()!=null)
{
print($node->getnext()->getdata());
if($node->getnext()->getnext()==null){break;}
$node=$node->getnext();
}
}
/**
*@author mzxy
*@param $data--要访问的节点的数据
* @param 此方法只是演示不具有实际意义
*
*/
public function getat($data)
{
$node=$this->header->getnext();
if($node->getnext()==null){
print("数据集为空!");
return;
}
while($node->getdata()!=$data)
{
if($node->getnext()==null){break;}
$node=$node->getnext();
}
return $node->getdata();
}
/**
*@author mzxy
*@param $value--需要更新的节点的原数据 --$initial---更新后的数据
*
*/
public function update($initial,$value)
{
$node=$this->header->getnext();
if($node->getnext()==null){
print("数据集为空!");
return;
}
while($node->getdata()!=$data)
{
if($node->getnext()==null){break;}
$node=$node->getnext();
}
$node->setdata($initial);
}
}
?>
希望本文所述对大家的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!