PHP implementation of a two-way linked list of a sample code

Source: Internet
Author: User
  1. /**
  2. * * * Double linked list
  3. * @author zhiyuan12@
  4. * @modified 2012-10-25
  5. * @site: bbs.it-home.org
  6. */
  7. /**
  8. * Linked list element node class
  9. */
  10. Class Node_element {
  11. Public $pre = NULL; Precursor
  12. Public $next = NULL; Subsequent
  13. Public $key = NULL; Element key value
  14. Public $data = NULL; Node value
  15. function __construct ($key, $data) {
  16. $this->key = $key;
  17. $this->data = $data;
  18. }
  19. }
  20. /**
  21. * Bidirectional linked List class
  22. */
  23. Class Doublelinkedlist {
  24. Private $head; Head pointer
  25. Private $tail; Tail hands
  26. Private $current; Current pointer
  27. Private $len; Linked list length
  28. function __construct () {
  29. $this->head = Self::_getnode (null, NULL);
  30. $this->curelement = $this->head;
  31. $this->tail = $this->head;
  32. $len = 0;
  33. }
  34. /**
  35. * @ desc: Read all nodes of the list
  36. */
  37. Public Function ReadAll () {
  38. $tmp = $this->head;
  39. while ($tmp->next!== null) {
  40. $tmp = $tmp->next;
  41. Var_dump ($tmp->key, $tmp->data);
  42. }
  43. }
  44. Public function Move ($pos 1, $pos 2) {
  45. $pos 1Node = $this->findposition ($pos 1);
  46. $pos 2Node = $this->findposition ($pos 2);
  47. if ($pos 1Node!== null && $pos 2Node!== null) {
  48. $tmpKey = $pos 1node->key;
  49. $tmpData = $pos 1node->data;
  50. $pos 1node->key = $pos 2node->key;
  51. $pos 1node->data = $pos 2node->data;
  52. $pos 2node->key = $tmpKey;
  53. $pos 2node->data = $tmpData;
  54. return true;
  55. }
  56. return false;
  57. }
  58. /**
  59. * @ desc: Delete node in specified keyword
  60. *
  61. * @param: $key
  62. * Specify the location of the linked list element key
  63. */
  64. Public Function Delete ($key) {
  65. $pos = $this->find ($key);
  66. if ($pos!== null) {
  67. $tmp = $pos;
  68. $last = null;
  69. $first = true;
  70. while ($tmp->next!== null && $tmp->next->key = = = $key) {
  71. $tmp = $tmp->next;
  72. if (! $first) {
  73. $this->delnode ($last);
  74. } else {
  75. $first = false;
  76. }
  77. $last = $tmp;
  78. }
  79. if ($tmp->next!== null) {
  80. $pos->pre->next = $tmp->next;
  81. $tmp->next->pre = $pos->pre;
  82. } else {
  83. $pos->pre->next = null;
  84. }
  85. $this->delnode ($pos);
  86. $this->delnode ($tmp);
  87. }
  88. }
  89. /**
  90. * @ desc: Delete node at specified location
  91. *
  92. * @param: $key
  93. * Specify the location of the linked list element key
  94. */
  95. Public Function Deleteposition ($pos) {
  96. $tmp = $this->findposition ($pos);
  97. if ($tmp = = = null) {
  98. return true;
  99. }
  100. if ($tmp = = = $this->gettail ()) {
  101. $tmp->pre->next = null;
  102. $this->delnode ($tmp);
  103. return true;
  104. }
  105. $tmp->pre->next = $tmp->next;
  106. $tmp->next->pre = $tmp->pre;
  107. $this->delnode ($tmp);
  108. }
  109. /**
  110. * @ desc: Insert a node before specifying a key value
  111. *
  112. * @param: $key
  113. *//list element of the specified position key
  114. * @param: $data
  115. *//Linked list element data to be inserted
  116. * @param: $flag
  117. *//whether to insert in order to find location
  118. */
  119. Public Function Insert ($key, $data, $flag = True) {
  120. $newNode = Self::_getnode ($key, $data);
  121. $tmp = $this->find ($key, $flag);
  122. if ($tmp!== null) {
  123. $newNode->pre = $tmp->pre;
  124. $newNode->next = $tmp;
  125. $tmp->pre = $newNode;
  126. $newNode->pre->next = $newNode;
  127. } else {
  128. $newNode->pre = $this->tail;
  129. $this->tail->next = $newNode;
  130. $this->tail = $newNode;
  131. }
  132. $this->len + +;
  133. }
  134. /**
  135. * @ desc: Insert a node before the specified position
  136. *
  137. * @param: $pos
  138. * Specify where to insert the linked list
  139. * @param: $key
  140. * Specify the location of the linked list element key
  141. * @param: $data
  142. * The linked list element data to be inserted
  143. */
  144. Public Function Insertposition ($pos, $key, $data) {
  145. $newNode = Self::_getnode ($key, $data);
  146. $tmp = $this->findposition ($pos);
  147. if ($tmp!== null) {
  148. $newNode->pre = $tmp->pre;
  149. $newNode->next = $tmp;
  150. $tmp->pre = $newNode;
  151. $newNode->pre->next = $newNode;
  152. } else {
  153. $newNode->pre = $this->tail;
  154. $this->tail->next = $newNode;
  155. $this->tail = $newNode;
  156. }
  157. $this->len + +;
  158. return true;
  159. }
  160. /**
  161. * @ desc: Query specified location data based on key value
  162. *
  163. * @param: $key
  164. *//list element of the specified position key
  165. * @param: $flag
  166. *//whether to search sequentially
  167. */
  168. Public function Find ($key, $flag = True) {
  169. if ($flag) {
  170. $tmp = $this->head;
  171. while ($tmp->next!== null) {
  172. $tmp = $tmp->next;
  173. if ($tmp->key = = = $key) {
  174. return $tmp;
  175. }
  176. }
  177. } else {
  178. $tmp = $this->gettail ();
  179. while ($tmp->pre!== null) {
  180. if ($tmp->key = = = $key) {
  181. return $tmp;
  182. }
  183. $tmp = $tmp->pre;
  184. }
  185. }
  186. return null;
  187. }
  188. /**
  189. * @ desc: Specify location data based on location query
  190. *
  191. * @param: $pos
  192. *//list element of the specified position key
  193. */
  194. Public Function Findposition ($pos) {
  195. if ($pos <= 0 | | $pos > $this->len)
  196. return null;
  197. if ($pos < ($this->LEN/2 + 1)) {
  198. $tmp = $this->head;
  199. $count = 0;
  200. while ($tmp->next!== null) {
  201. $tmp = $tmp->next;
  202. $count + +;
  203. if ($count = = = $pos) {
  204. return $tmp;
  205. }
  206. }
  207. } else {
  208. $tmp = $this->tail;
  209. $pos = $this->len-$pos + 1;
  210. $count = 1;
  211. while ($tmp->pre!== null) {
  212. if ($count = = = $pos) {
  213. return $tmp;
  214. }
  215. $tmp = $tmp->pre;
  216. $count + +;
  217. }
  218. }
  219. return null;
  220. }
  221. /**
  222. * @ desc: Return to the list header node
  223. */
  224. Public Function GetHead () {
  225. return $this->head->next;
  226. }
  227. /**
  228. * @ desc: Returns the link footer node
  229. */
  230. Public Function GetTail () {
  231. return $this->tail;
  232. }
  233. /**
  234. * @ desc: Query the number of linked list nodes
  235. */
  236. Public Function GetLength () {
  237. return $this->len;
  238. }
  239. private static function _getnode ($key, $data) {
  240. $newNode = new Node_element ($key, $data);
  241. if ($newNode = = = null) {
  242. echo "New node fail!";
  243. }
  244. return $newNode;
  245. }
  246. Private Function Delnode ($node) {
  247. Unset ($node);
  248. $this->len--;
  249. }
  250. }
  251. $myList = new Doublelinkedlist ();
  252. $myList->insert (1, "test1");
  253. $myList->insert (2, "test2");
  254. $myList->insert ("2b", "test2-b");
  255. $myList->insert (2, "test2-c");
  256. $myList->insert (3, "test3");
  257. $myList->insertposition (5, "T", "TESTT");
  258. $myList->readall ();
  259. echo "+ + +";
  260. $myList->deleteposition (0);
  261. $myList->readall ();
  262. echo "...". $myList->getlength ();
  263. Var_dump ($myList->findposition (3)->data);
  264. ?>
Copy Code
  • Related Article

    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.