Analysis of php linked list Usage examples

Source: Internet
Author: User
Analysis of php linked list Usage examples

This example describes the usage of the php linked list. Share it with you for your reference. The details are as follows:

This section briefly introduces the basic usage of php linked list, including creating, traversing, and updating 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
  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 -- 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 -- 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 traversal
  86. *
  87. */
  88. Public function get ()
  89. {
  90. $ Node = $ this-> header;
  91. If ($ node-> getNext () = null ){
  92. Print ("The dataset is empty! ");
  93. Return;
  94. }
  95. While ($ node-> getNext ()! = Null)
  96. {
  97. Print ($ node-> getNext ()-> getData ());
  98. If ($ node-> getNext () = null) {break ;}
  99. $ Node = $ node-> getNext ();
  100. }
  101. }
  102. /**
  103. * @ Author MzXy
  104. * @ Param $ data -- data of the node to be accessed
  105. * @ Param this method only demonstrates that it is not of practical significance.
  106. *
  107. */
  108. Public function getAt ($ data)
  109. {
  110. $ Node = $ this-> header-> getNext ();
  111. If ($ node-> getNext () = null ){
  112. Print ("The dataset 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 -- original data of the node to be updated -- $ initial --- updated data
  125. *
  126. */
  127. Public function update ($ initial, $ value)
  128. {
  129. $ Node = $ this-> header-> getNext ();
  130. If ($ node-> getNext () = null ){
  131. Print ("The dataset 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. ?>

I hope this article will help you with php programming.

Linked list, php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.