php链表用法实例分析

php中文网
发布: 2016-07-25 08:44:54
原创
1022人浏览过

本文实例讲述了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程序设计有所帮助。

飞书多维表格
飞书多维表格

表格形态的AI工作流搭建工具,支持批量化的AI创作与分析任务,接入DeepSeek R1满血版

飞书多维表格 26
查看详情 飞书多维表格
链表, php


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号