搜索
首页后端开发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 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

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)

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

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具