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

PHP实现的静态链表

程序员文章站 2022-05-03 23:25:38
...
PHP实现的单链表,如下代码:
  1. //结点类
  2. class Node{
  3. private $next = NULL; //下一个结点指针
  4. private $data = NULL; //数据
  5. public function Node($data, $next = NULL){
  6. $this->data = $data;
  7. $next && $this->next = $next;
  8. }
  9. public function getData(){
  10. return $this->data;
  11. }
  12. public function setData($data){
  13. $this->data = $data;
  14. }
  15. public function getNext(){
  16. return $this->next;
  17. }
  18. public function setNext($next){
  19. $this->next = $next;
  20. }
  21. }
  22. //单链表类
  23. class LinkList{
  24. private $data_list = NULL; //结点集
  25. public function LinkList($data = false){
  26. $this->data_list = array();
  27. $title = new Node(NULL);
  28. $this->data_list[] = $title;
  29. if($data){
  30. if(is_array($data)){
  31. $this->addMoreData($data);
  32. }else{
  33. $this->addData($data);
  34. }
  35. }
  36. }
  37. //返回第N个结点的值
  38. public function getNodeByNumber($number){
  39. return $this->data_list[$this->findKeyByNumber($number)]->getData();
  40. }
  41. //添加一组结点
  42. public function addMoreData($datas){
  43. foreach($datas as $value){
  44. $this->addData($value);
  45. }
  46. }
  47. //添加结点统一入口,供外面调用
  48. //$number 添加在第几个结点的后面
  49. public function addData($data, $number = false){
  50. $node = new Node($data);
  51. if($number === FALSE $number == count($this->data_list)){
  52. $this->insertLastNode($node);
  53. }elseif($number > count($this->data_list)){
  54. return false;
  55. }else{
  56. $this->insertNode($node, $number);
  57. }
  58. }
  59. //插入一个结点到最后
  60. private function insertLastNode($node){
  61. $node->setNext(NULL);
  62. $lastKey = $this->findLastNode();
  63. $insert_key = $this->insertNodeIntoArray($node);
  64. $this->data_list[$lastKey]->setNext($insert_key);
  65. }
  66. //插入一个结点
  67. private function insertNode($node, $number){
  68. $insert_number = $this->findKeyByNumber($number);
  69. $node->setNext($this->data_list[$insert_number]->getNext());
  70. $insert_key = $this->insertNodeIntoArray($node);
  71. $this->data_list[$insert_number]->setNext($insert_key);
  72. }
  73. //查找第N个结点对应的数组key
  74. private function findKeyByNumber($number){
  75. $i = $key = 0;
  76. while($i $number){
  77. $key = $this->data_list[$key]->getNext();
  78. $i ++;
  79. }
  80. return $key;
  81. }
  82. //将结点加入数组
  83. private function insertNodeIntoArray($node){
  84. $this->data_list[] = $node;
  85. return $this->getLastKey();
  86. }
  87. //删除结点
  88. public function deleteNode($number){
  89. if($number == 0 $number > count($this->data_list)){
  90. return false;
  91. }
  92. $pre_key = $this->findKeyByNumber($number - 1);
  93. $key = $this->data_list[$pre_key]->getNext();
  94. $this->data_list[$pre_key]->setNext($this->data_list[$key]->getNext());
  95. unset($this->data_list[$key]);
  96. }
  97. //查找某结点的前一个结点
  98. private function getPreNodeKey($key){
  99. foreach($this->data_list as $k=>$v){
  100. if($v->getNext() == $key){
  101. return $k;
  102. }
  103. }
  104. return false;
  105. }
  106. //打印链表
  107. public function getData_list(){
  108. return $this->data_list;
  109. }
  110. //返回数组的最后一个键
  111. private function getLastKey(){
  112. end($this->data_list);
  113. return key($this->data_list);
  114. }
  115. //判断某个键值是否存在
  116. private function ifExistKey($key){
  117. if(array_key_exists($key, $this->data_list)){
  118. return true;
  119. }
  120. return false;
  121. }
  122. //查找尾结点
  123. public function findLastNode(){
  124. foreach($this->data_list as $key=>$value){
  125. if($value->getNext() === NULL){
  126. return $key;
  127. }
  128. }
  129. }
  130. }
  131. ?>