欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php链表用法实例分析

程序员文章站 2022-05-28 15:01:40
...

本文实例讲述了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