Home  >  Article  >  Backend Development  >  Example analysis of php linked list usage

Example analysis of php linked list usage

WBOY
WBOYOriginal
2016-07-25 08:44:54748browse

The example in this article describes the usage of php linked list. Share it with everyone for your reference. The details are as follows:

Here is a brief introduction to the basic usage of PHP linked lists, including the creation, traversal, and update of linked list nodes.

  1. /**
  2. * @author MzXy
  3. * @copyright 2011
  4. * @param PHP linked list
  5. */
  6. /**
  7. *
  8. *Node class
  9. */
  10. class Node
  11. {
  12. private $Data;//Node data
  13. private $Next;//Next Node
  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. }//Function class
  31. class LinkList
  32. {
  33. private $header;//Header node
  34. private $size;//Length
  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--the data of the node to be added
  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--the data of the node to be removed
  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 traverse
  86. *
  87. */
  88. public function get()
  89. {
  90. $node=$this->header;
  91. if($node->getNext()==null){
  92. print("The data set is empty!");
  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--the data of the node to be accessed
  105. * @param This method is just for demonstration and has no practical significance
  106. *
  107. */
  108. public function getAt($data)
  109. {
  110. $node= $this->header->getNext();
  111. if($node->getNext()==null){
  112. print("The data set is empty!");
  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--the original data of the node that needs to be updated --$initial---the updated data
  125. *
  126. */
  127. public function update($initial,$value)
  128. {
  129. $node=$this->header->getNext();
  130. if($node->getNext()==null){
  131. print("The data set is empty!");
  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. ?>
Copy code

I hope this article will be helpful to everyone’s PHP programming design.

Linked list, php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn