html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP实现的静态链表_PHP教程_编程技术

[ ] 已经帮助:人解决问题

PHP实现的单链表,如下代码:2hAHTML5中文学习网 - HTML5先行者学习网

  1. <?php 
  2.     //结点类 
  3.     class Node{ 
  4.         private $next = NULL; //下一个结点指针 
  5.         private $data = NULL; //数据 
  6.     
  7.         public function Node($data$next = NULL){ 
  8.             $this->data = $data
  9.             $next && $this->next = $next
  10.         } 
  11.         public function getData(){ 
  12.             return $this->data; 
  13.         } 
  14.         public function setData($data){ 
  15.             $this->data = $data
  16.         } 
  17.         public function getNext(){ 
  18.             return $this->next; 
  19.         } 
  20.         public function setNext($next){ 
  21.             $this->next = $next
  22.         } 
  23.     } 
  24.  
  25.     //单链表类 
  26.     class LinkList{ 
  27.         private $data_list = NULL; //结点集           
  28.  
  29.         public function LinkList($data = false){ 
  30.             $this->data_list = array(); 
  31.             $title = new Node(NULL); 
  32.             $this->data_list[] = $title
  33.            
  34.             if($data){ 
  35.                 if(is_array($data)){ 
  36.                     $this->addMoreData($data); 
  37.                 }else
  38.                     $this->addData($data); 
  39.                 } 
  40.             } 
  41.         } 
  42.         //返回第N个结点的值 
  43.         public function getNodeByNumber($number){ 
  44.             return $this->data_list[$this->findKeyByNumber($number)]->getData(); 
  45.         } 
  46.         //添加一组结点 
  47.         public function addMoreData($datas){ 
  48.             foreach($datas as $value){ 
  49.                 $this->addData($value); 
  50.             } 
  51.         } 
  52.         //添加结点统一入口,供外面调用 
  53.         //$number 添加在第几个结点的后面 
  54.         public function addData($data$number = false){ 
  55.             $node = new Node($data); 
  56.             if($number === FALSE  $number == count($this->data_list)){ 
  57.                 $this->insertLastNode($node); 
  58.             }elseif($number > count($this->data_list)){ 
  59.                 return false; 
  60.             }else
  61.                 $this->insertNode($node$number); 
  62.             } 
  63.         } 
  64.         //插入一个结点到最后 
  65.         private function insertLastNode($node){ 
  66.                 $node->setNext(NULL);              
  67.                 $lastKey = $this->findLastNode(); 
  68.                 $insert_key = $this->insertNodeIntoArray($node); 
  69.                 $this->data_list[$lastKey]->setNext($insert_key); 
  70.         }     
  71.         //插入一个结点 
  72.         private function insertNode($node$number){ 
  73.             $insert_number = $this->findKeyByNumber($number);     
  74.             $node->setNext($this->data_list[$insert_number]->getNext()); 
  75.             $insert_key = $this->insertNodeIntoArray($node); 
  76.             $this->data_list[$insert_number]->setNext($insert_key); 
  77.         } 
  78.         //查找第N个结点对应的数组key 
  79.         private function findKeyByNumber($number){ 
  80.             $i = $key = 0; 
  81.  
  82.             while($i < $number){ 
  83.                 $key = $this->data_list[$key]->getNext(); 
  84.                 $i ++; 
  85.             } 
  86.           
  87.             return $key
  88.         } 
  89.         //将结点加入数组 
  90.         private function insertNodeIntoArray($node){ 
  91.             $this->data_list[] = $node;      
  92.             return $this->getLastKey(); 
  93.         } 
  94.         //删除结点 
  95.         public function deleteNode($number){ 
  96.             if($number == 0  $number > count($this->data_list)){ 
  97.                 return false; 
  98.             } 
  99.  
  100.             $pre_key = $this->findKeyByNumber($number - 1); 
  101.             $key = $this->data_list[$pre_key]->getNext(); 
  102.  
  103.           $this->data_list[$pre_key]->setNext($this->data_list[$key]->getNext()); 
  104.             unset($this->data_list[$key]); 
  105.         } 
  106.  
  107.         //查找某结点的前一个结点 
  108.         private function getPreNodeKey($key){ 
  109.             foreach($this->data_list as $k=>$v){ 
  110.                 if($v->getNext() == $key){ 
  111.                     return $k;   
  112.                 } 
  113.             } 
  114.             return false; 
  115.         } 
  116.  
  117.         //打印链表 
  118.         public function getData_list(){ 
  119.             return $this->data_list; 
  120.         } 
  121.  
  122.         //返回数组的最后一个键 
  123.         private function getLastKey(){ 
  124.             end($this->data_list); 
  125.             return key($this->data_list); 
  126.         } 
  127.  
  128.         //判断某个键值是否存在 
  129.         private function ifExistKey($key){ 
  130.             if(array_key_exists($key$this->data_list)){ 
  131.                 return true; 
  132.             }           
  133.             return false; 
  134.         } 
  135.         //查找尾结点 
  136.         public function findLastNode(){ 
  137.             foreach($this->data_list as $key=>$value){ 
  138.                 if($value->getNext() === NULL){ 
  139.                     return $key
  140.                 } 
  141.             } 
  142.         } 
  143.     } 
  144. ?> 
2hAHTML5中文学习网 - HTML5先行者学习网
2hAHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助