首頁  >  文章  >  後端開發  >  PHP實作雙向鍊錶的一例程式碼

PHP實作雙向鍊錶的一例程式碼

WBOY
WBOY原創
2016-07-25 08:57:401259瀏覽
  1. /**
  2. * **雙向鍊錶
  3. * @author zhiyuan12@
  4. * @modified 2012-10-25
  5. * @site: bbs.it-home.org
  6. */
  7. /**
  8. * 鍊錶元素結點類別
  9. */
  10. class Node_Element {
  11. public $pre = NULL; // 前驅
  12. public $next = NULL; // 後繼
  13. public $key = NULL; // 元素鍵值
  14. public $data = NULL; // 結點值
  15. function __Construct($key, $data) {
  16. $this->key = $key;
  17. $this->data = $data;
  18. }
  19. }
  20. /**
  21. * 雙向鍊錶類別
  22. */
  23. class DoubleLinkedList {
  24. private $head; // 頭指標
  25. private $tail; // 尾指標
  26. private $current; // 目前指標
  27. private $ // 鍊錶長度
  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: 讀取鍊錶全部結點
  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($pos1, $pos2) {
  45. $pos1Node = $this->findPosition ( $pos1 );
  46. $pos2Node = $this->findPosition ( $pos2 );
  47. if ($pos1Node !== null && $pos2Node !== null) {
  48. $tmpKey = $pos1Node->key;
  49. $tmpData = $pos1Node->data;
  50. $pos1Node->key = $pos2Node->key;
  51. $pos1Node->data = $pos2Node->data;
  52. $pos2Node->key = $tmpKey;
  53. $pos2Node->data = $tmpData;
  54. return true;
  55. }
  56. return false;
  57. }
  58. /**
  59. * @ desc: 在指定關鍵字刪除結點
  60. *
  61. * @param : $key
  62. * 指定位置的鍊錶元素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: 在指定位置刪除結點
  91. *
  92. * @param : $key
  93. * 指定位置的鍊錶元素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: 在指定鍵值之前插入結點
  111. *
  112. * @param : $key
  113. * //指定位置的鍊錶元素key
  114. * @param : $data * //要插入的鍊錶元素資料
  115. * @param : $flag
  116. * //是否順序查找位置進行插入
  117. */
  118. public function insert($key, $data, $ flag = true) {
  119. $newNode = self::_getNode ( $key, $data );
  120. $tmp = $this->find ( $key, $flag );
  121. if ($tmp ! == null) {
  122. $newNode->pre = $tmp->pre;
  123. $newNode->next = $tmp;
  124. $tmp->pre = $newNode;
  125. $newNode- >pre->next = $newNode;
  126. }else {
  127. $newNode->pre = $this->tail;
  128. $this->tail->next = $newNode;
  129. $this->tail = $newNode;
  130. }
  131. $this->len ++;
  132. }
  133. /**
  134. * @ desc: 在指定位置之前插入結點
  135. *
  136. * @param : $pos
  137. * 指定插入鍊錶的位置
  138. * @param : $key
  139. * 指定位置的鍊錶元素key
  140. * @param : $data
  141. * 要插入的鍊錶元素資料
  142. */
  143. public function insertPosition($pos, $key, $data) {
  144. $newNode = self::_getNode ( $key, $data );
  145. $tmp = $this->findPosition ( $pos );
  146. if ($tmp !== null) {
  147. $newNode->pre = $tmp->pre;
  148. $newNode->next = $tmp;
  149. $tmp->pre = $newNode;
  150. $newNode->pre->next = $newNode;
  151. } else {
  152. $newNode->pre = $this->tail;
  153. $this->tail->next = $newNode;
  154. $this->tail = $newNode;
  155. }
  156. $this->len ++;
  157. 回傳true;
  158. }
  159. /**
  160. * @ desc: 根據key值查詢指定位置資料
  161. *
  162. * @param : $key
  163. * //指定位置的鍊錶元素key
  164. * @param : $flag
  165. * //是否順序查找
  166. */
  167. public function find($key, $flag = true) {
  168. if ($flag) {
  169. $ tmp = $this->頭;
  170. while ( $tmp->next !== null ) {
  171. $tmp = $tmp->next;
  172. if ($tmp->key === $key) {
  173. return $tmp;
  174. }
  175. }
  176. } else {
  177. $tmp = $this->getTail ();
  178. while ( $tmp->pre !== null ) {
  179. if ($tmp->key === $key) {
  180. return $tmp; }
  181. }
  182. $tmp = $tmp->pre;
  183. }
  184. }
  185. 回傳 null;
  186. }
  187. /**
  188. * @ desc: 根據位置查詢指定位置資料
  189. *
  190. * @param : $pos
  191. * //指定位置的鍊錶元素key
  192. */
  193. public function findPosition($pos) {
  194. if ($pos $this->len)
  195. 返回null;
  196. if ($pos len / 2 + 1)) {
  197. $tmp = $this->head;
  198. $count = 0;
  199. while ( $tmp->next !== null ) {
  200. $tmp = $tmp->next;
  201. $count ++;
  202. if ($count === $pos) {
  203. return $tmp;
  204. }
  205. }
  206. } else {
  207. $tmp = $this->tail;
  208. $pos = $this->len - $pos + 1;
  209. $count = 1;
  210. while ( $tmp->pre !== null ) {
  211. if ($count === $pos) {
  212. return $tmp;
  213. }
  214. $tmp = $tmp->pre;
  215. $count ++;
  216. }
  217. }
  218. 回傳 null;
  219. }
  220. /**
  221. * @ desc: 返回鍊錶頭節點
  222. */
  223. public function getHead() {
  224. return $this->head->next ;
  225. }
  226. /**
  227. * @ desc: 返回鍊錶尾節點
  228. */
  229. public function getTail() {
  230. return $this->tail;
  231. }
  232. /**
  233. * @ desc: 查詢鍊錶節點個數
  234. */
  235. public function getLength() {
  236. return $this->len;
  237. }
  238. 真空負極函數 _getNode($key, $data) {
  239. $newNode = new Node_Element ( $key, $data );
  240. if ($newNode === null) {
  241. echo "新節點失敗!";
  242. }
  243. return $newNode;
  244. }
  245. private function delNode($node) {
  246. unset ( $node );
  247. $this->len --;
  248. }
  249. }
  250. // $myList = new DoubleLinkedList ();
  251. // $myList->insert ( 1, "test1" );
  252. // $myList->insert ( 2, "test2" );
  253. // $myList->insert ( "2b", "test2-b" );
  254. // $myList->insert ( 2, "test2-c" );
  255. // $myList->insert ( 3, "test3" );
  256. // $myList->insertPosition ( 5, "t", "testt" );
  257. // $myList->readAll();
  258. // echo "+++";
  259. // $myList->deletePosition(0);
  260. // $myList->readAll();
  261. //回顯“...”。
  262. // var_dump ( $myList->findPosition ( 3 )->data );
  263. 複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn