国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

php鏈表用法實(shí)例分析

2024-05-04 23:37:32
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了php鏈表用法,實(shí)例分析了php創(chuàng)建鏈表及針對(duì)鏈表節(jié)點(diǎn)的增加、刪除、更新與遍歷等常用操作,需要的朋友可以參考下

本文實(shí)例講述了php鏈表用法。分享給大家供大家參考。具體如下:

這里簡(jiǎn)單介紹了php鏈表的基本用法,包括鏈表節(jié)點(diǎn)的創(chuàng)建、遍歷、更新等操作。

 

 
  1. <?php 
  2. /** 
  3. * @author MzXy 
  4. * @copyright 2011 
  5. * @param PHP鏈表 
  6. */ 
  7. /** 
  8. * 
  9. *節(jié)點(diǎn)類 
  10. */ 
  11. class Node 
  12. private $Data;//節(jié)點(diǎn)數(shù)據(jù) 
  13. private $Next;//下一節(jié)點(diǎn) 
  14. public function setData($value){ 
  15. $this->Data=$value; 
  16. public function setNext($value){ 
  17. $this->Next=$value; 
  18. }  
  19. public function getData(){ 
  20. return $this->Data; 
  21. public function getNext(){ 
  22. return $this->Next; 
  23. public function __construct($data,$next){ 
  24. $this->setData($data); 
  25. $this->setNext($next); 
  26. }//功能類 
  27. class LinkList 
  28. private $header;//頭節(jié)點(diǎn) 
  29. private $size;//長(zhǎng)度 
  30. public function getSize(){ 
  31. $i=0; 
  32. $node=$this->header; 
  33. while($node->getNext()!=null
  34. { $i++; 
  35. $node=$node->getNext(); 
  36. return $i; 
  37. public function setHeader($value){ 
  38. $this->header=$value; 
  39. public function getHeader(){ 
  40. return $this->header; 
  41. public function __construct(){ 
  42. header("content-type:text/html; charset=utf-8"); 
  43. $this->setHeader(new Node(null,null)); 
  44. /** 
  45. *@author MzXy 
  46. *@param $data--要添加節(jié)點(diǎn)的數(shù)據(jù) 
  47.  
  48. */ 
  49. public function add($data) 
  50. $node=$this->header; 
  51. while($node->getNext()!=null
  52. $node=$node->getNext(); 
  53. $node->setNext(new Node($data,null)); 
  54. /** 
  55. *@author MzXy 
  56. *@param $data--要移除節(jié)點(diǎn)的數(shù)據(jù) 
  57.  
  58. */ 
  59. public function removeAt($data) 
  60. $node=$this->header; 
  61. while($node->getData()!=$data) 
  62. $node=$node->getNext(); 
  63. $node->setNext($node->getNext()); 
  64. $node->setData($node->getNext()->getData()); 
  65. /** 
  66. *@author MzXy 
  67. *@param 遍歷 
  68.  
  69. */ 
  70. public function get() 
  71. $node=$this->header; 
  72. if($node->getNext()==null){ 
  73. print("數(shù)據(jù)集為空!"); 
  74. return
  75. while($node->getNext()!=null
  76. print($node->getNext()->getData()); 
  77. if($node->getNext()->getNext()==null){break;} 
  78. $node=$node->getNext(); 
  79. /** 
  80. *@author MzXy 
  81. *@param $data--要訪問(wèn)的節(jié)點(diǎn)的數(shù)據(jù) 
  82. * @param 此方法只是演示不具有實(shí)際意義 
  83.  
  84. */ 
  85. public function getAt($data) 
  86. $node=$this->header->getNext(); 
  87. if($node->getNext()==null){ 
  88. print("數(shù)據(jù)集為空!"); 
  89. return
  90. while($node->getData()!=$data) 
  91. if($node->getNext()==null){break;} 
  92. $node=$node->getNext(); 
  93. return $node->getData();  
  94. /** 
  95. *@author MzXy 
  96. *@param $value--需要更新的節(jié)點(diǎn)的原數(shù)據(jù) --$initial---更新后的數(shù)據(jù) 
  97.  
  98. */ 
  99. public function update($initial,$value) 
  100. $node=$this->header->getNext(); 
  101. if($node->getNext()==null){ 
  102. print("數(shù)據(jù)集為空!"); 
  103. return
  104. while($node->getData()!=$data) 
  105. if($node->getNext()==null){break;} 
  106. $node=$node->getNext(); 
  107. $node->setData($initial);  
  108. ?> 

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 镇坪县| 宁阳县| 大冶市| 丰都县| 新化县| 雷山县| 临安市| 博乐市| 英山县| 刚察县| 梅河口市| 淮阳县| 昭通市| 徐汇区| 蓬溪县| 垣曲县| 仙居县| 岳阳县| 大渡口区| 社会| 康乐县| 玛纳斯县| 山阴县| 石景山区| 桃园县| 永安市| 台北县| 海盐县| 酒泉市| 龙泉市| 慈利县| 临武县| 平乐县| 崇阳县| 房山区| 竹溪县| 逊克县| 鹿泉市| 崇仁县| 阜城县| 大足县|