当前位置:首页 > PHP教程 > php应用 > 列表

php链表用法实例分析

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-10 10:39:39 浏览: 评论:0 

这篇文章主要介绍了php链表用法,实例分析了php创建链表及针对链表节点的增加、删除、更新与遍历等常用操作,需要的朋友可以参考下。

本文实例讲述了php链表用法,分享给大家供大家参考,具体如下:

这里简单介绍了php链表的基本用法,包括链表节点的创建、遍历、更新等操作。

  1. <?php 
  2. /** 
  3.  * @author MzXy 
  4.  * @copyright 2011 
  5.  * @param PHP链表 
  6.  */ 
  7. /** 
  8. * 
  9. *节点类 
  10. */ 
  11. class Node 
  12. { 
  13.   private $Data;//节点数据 
  14.   private $Next;//下一节点 
  15.   public function setData($value){ 
  16.     $this->Data=$value; 
  17.   } 
  18.   public function setNext($value){ 
  19.      $this->Next=$value; 
  20.   }   
  21.   public function getData(){ 
  22.     return $this->Data; 
  23.   } 
  24.   public function getNext(){ 
  25.     return $this->Next; 
  26.   } 
  27.   public function __construct($data,$next){ 
  28.     $this->setData($data); 
  29.     $this->setNext($next); 
  30.   } 
  31. }//功能类 
  32. class LinkList 
  33. { 
  34.   private $header;//头节点 
  35.   private $size;//长度 
  36.   public function getSize(){ 
  37.     $i=0; 
  38.     $node=$this->header; 
  39.     while($node->getNext()!=null) 
  40.     {  $i++; 
  41.       $node=$node->getNext(); 
  42.     } 
  43.    return $i; 
  44.   } 
  45.   public function setHeader($value){ 
  46.     $this->header=$value; 
  47.   } 
  48.   public function getHeader(){ 
  49.     return $this->header; 
  50.   } 
  51.   public function __construct(){ 
  52.      header("content-type:text/html; charset=utf-8"); 
  53.     $this->setHeader(new Node(null,null)); 
  54.   } 
  55.   /** 
  56.   *@author MzXy 
  57.   *@param $data--要添加节点的数据 
  58.   *  
  59.   */ 
  60.   public function add($data) 
  61.   { 
  62.     $node=$this->header; 
  63.     while($node->getNext()!=null) 
  64.     { 
  65.       $node=$node->getNext(); 
  66.     } 
  67.     $node->setNext(new Node($data,null)); 
  68.   } 
  69.    /** 
  70.   *@author MzXy 
  71.   *@param $data--要移除节点的数据 
  72.   *  
  73.   */ 
  74.   public function removeAt($data) 
  75.   { 
  76.     $node=$this->header; 
  77.     while($node->getData()!=$data) 
  78.     { 
  79.       $node=$node->getNext(); 
  80.     } 
  81.     $node->setNext($node->getNext()); 
  82.     $node->setData($node->getNext()->getData()); 
  83.   } 
  84.    /** 
  85.   *@author MzXy 
  86.   *@param 遍历 
  87.   *  
  88.   */ 
  89.   public function get() 
  90.   { 
  91.     $node=$this->header; 
  92.     if($node->getNext()==null){ 
  93.       print("数据集为空!"); 
  94.       return; 
  95.     } 
  96.     while($node->getNext()!=null) 
  97.     { 
  98.       print($node->getNext()->getData()); 
  99.       if($node->getNext()->getNext()==null){break;} 
  100.       $node=$node->getNext(); 
  101.     } 
  102.   } 
  103.    /** 
  104.   *@author MzXy 
  105.   *@param $data--要访问的节点的数据 
  106.   * @param 此方法只是演示不具有实际意义 
  107.   *  
  108.   */ 
  109.   public function getAt($data) 
  110.   { 
  111.     $node=$this->header->getNext(); 
  112.      if($node->getNext()==null){ 
  113.       print("数据集为空!"); 
  114.       return; 
  115.     } 
  116.     while($node->getData()!=$data) 
  117.     { 
  118.       if($node->getNext()==null){break;} 
  119.       $node=$node->getNext(); 
  120.     } 
  121.     return $node->getData();     
  122.   } 
  123.    /** 
  124.   *@author MzXy 
  125.   *@param $value--需要更新的节点的原数据 --$initial---更新后的数据 
  126.   *  
  127.   */ 
  128.   public function update($initial,$value) 
  129.   { 
  130.      $node=$this->header->getNext(); 
  131.      if($node->getNext()==null){ 
  132.       print("数据集为空!"); 
  133.       return; 
  134.     } 
  135.     while($node->getData()!=$data) 
  136.     { 
  137.       if($node->getNext()==null){break;} 
  138.       $node=$node->getNext(); 
  139.     } 
  140.      $node->setData($initial);    
  141.   } 
  142. } 
  143. ?>

Tags: php链表

分享到: