搜尋
首頁後端開發php教程php鍊錶用法實例分析

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

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

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

希望本文所述对大家的php程序设计有所帮助。

鍊錶, php


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。