Home Backend Development PHP Tutorial Example analysis of php linked list usage

Example analysis of php linked list usage

Jul 25, 2016 am 08:44 AM

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. <?php
  2. /**
  3. * @author MzXy
  4. * @copyright 2011
  5. * @param PHP linked list
  6. */
  7. /**
  8. *
  9. *Node class
  10. */
  11. class Node
  12. {
  13. private $Data;//Node data
  14. private $Next;//Next Node
  15. public function setData($value){
  16. $this->Data=$value;
  17. }
  18. public function setNext($value){
  19. $this->Next=$value;
  20. }
  21. public function getData( ){
  22. return $this->Data;
  23. }
  24. public function getNext(){
  25. return $this->Next;
  26. }
  27. public function __construct($data,$next){
  28. $this->setData ($data);
  29. $this->setNext($next);
  30. }
  31. }//Function class
  32. class LinkList
  33. {
  34. private $header;//Header node
  35. private $size;//Length
  36. public function getSize(){
  37. $i=0;
  38. $node=$this->header;
  39. while($node->getNext()!=null)
  40. { $i++;
  41. $node=$node-> getNext();
  42. }
  43. return $i;
  44. }
  45. public function setHeader($value){
  46. $this->header=$value;
  47. }
  48. public function getHeader(){
  49. return $this->header ;
  50. }
  51. public function __construct(){
  52. header("content-type:text/html; charset=utf-8");
  53. $this->setHeader(new Node(null,null));
  54. }
  55. /**
  56. *@author MzXy
  57. *@param $data--the data of the node to be added
  58. *
  59. */
  60. public function add($data)
  61. {
  62. $node=$this->header;
  63. while($node->getNext()!=null)
  64. {
  65. $node= $node->getNext();
  66. }
  67. $node->setNext(new Node($data,null));
  68. }
  69. /**
  70. *@author MzXy
  71. *@param $data--the data of the node to be removed
  72. *
  73. */
  74. public function removeAt($data)
  75. {
  76. $node=$this->header;
  77. while($node->getData()!=$data)
  78. {
  79. $node=$node->getNext();
  80. }
  81. $node-> setNext($node->getNext());
  82. $node->setData($node->getNext()->getData());
  83. }
  84. /**
  85. *@author MzXy
  86. *@param traverse
  87. *
  88. */
  89. public function get()
  90. {
  91. $node=$this->header;
  92. if($node->getNext()==null){
  93. print("The data set is empty!");
  94. return;
  95. }
  96. while($node->getNext()!=null)
  97. {
  98. print($node->getNext()->getData());
  99. if($node->getNext()->getNext ()==null){break;}
  100. $node=$node->getNext();
  101. }
  102. }
  103. /**
  104. *@author MzXy
  105. *@param $data--the data of the node to be accessed
  106. * @param This method is just for demonstration and has no practical significance
  107. *
  108. */
  109. public function getAt($data)
  110. {
  111. $node= $this->header->getNext();
  112. if($node->getNext()==null){
  113. print("The data set is empty!");
  114. return;
  115. }
  116. while($ node->getData()!=$data)
  117. {
  118. if($node->getNext()==null){break;}
  119. $node=$node->getNext();
  120. }
  121. return $node->getData();
  122. }
  123. /**
  124. *@author MzXy
  125. *@param $value--the original data of the node that needs to be updated --$initial---the updated data
  126. *
  127. */
  128. public function update($initial,$value)
  129. {
  130. $node=$this->header->getNext();
  131. if($node->getNext()==null){
  132. print("The data set is empty!");
  133. return;
  134. }
  135. while($node->getData()!=$data)
  136. {
  137. if($node->getNext()==null){break;}
  138. $node=$node->getNext();
  139. }
  140. $node->setData($initial);
  141. }
  142. }
  143. ?>
Copy code

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

Linked list, php


Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles