ホームページ  >  記事  >  バックエンド開発  >  PHP で二重リンクリストを実装するためのサンプルコード

PHP で二重リンクリストを実装するためのサンプルコード

WBOY
WBOYオリジナル
2016-07-25 08:57:401213ブラウズ
  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 $next = NULL;後継者
  12. public $key = NULL; // 要素キー値
  13. public $data = NULL; // ノード値
  14. function __Construct($key, $data) {
  15. $this->key = $key; >data = $data; }
  16. }
  17. /**
  18. * 二重リンクリストクラス
  19. */
  20. private $head; // 先頭ポインタ
  21. private $tail; // 現在のpointer
  22. private $len; // リンクリストの長さ
  23. function __Construct() {
  24. $this->head = self::_getNode ( null, null );
  25. $this->tail = $this->head;
  26. $len = 0;
  27. }
  28. /**
  29. * @ desc: リンクされたリストのすべてのノードを読み取ります
  30. */
  31. $tmp = $this->head;
  32. while ( $tmp->next !== null ) {
  33. $tmp = $tmp->next;
  34. var_dump ( $tmp->key, $tmp->data );
  35. public function move($pos1, $pos2) {
  36. $pos1Node = $this->findPosition ( $pos1 );
  37. $pos2Node = $this->findPosition ( $pos2 ); if ($pos1Node !== null && $pos2Node !== null) {
  38. $tmpKey = $pos1Node->key;
  39. $tmpData = $pos1Node->data;
  40. $pos1Node->key; ;data = $pos2Node->data;
  41. $pos2Node->key = $tmpData;
  42. true を返す
  43. }
  44. /**
  45. * @ desc: 指定したキーワードのノードを削除
  46. *
  47. * @param: $key
  48. * 指定した位置のリンクリスト要素キー
  49. */
  50. public function delete($key) {
  51. $pos = $this->find ( $key );
  52. if ($pos !== null) {
  53. $tmp = $pos = null; ;
  54. $first = true;
  55. while ( $tmp->next !== null && $tmp->next->key === $key ) {
  56. $tmp = $tmp->next; if (! $first) {
  57. $this->delNode ( $last );
  58. $first = false; }
  59. $last = $tmp;
  60. if ($tmp->next ! == null) {
  61. $pos->pre->next = $tmp->next;
  62. $tmp->next->pre = $pos->pre
  63. } else {
  64. $pos; ->pre->next = null;
  65. $this->delNode ( $pos );
  66. }
  67. }
  68. /**
  69. * @ desc: 指定位置のノードを削除
  70. *
  71. * @param: $key
  72. * 指定位置のリンクリスト要素のキー
  73. */
  74. public function deletePosition($pos) {
  75. $tmp = $this->findPosition ( $pos );
  76. if ($tmp === null) {
  77. return true
  78. }
  79. if ($tmp === $ this->getTail ()) {
  80. $tmp->pre->next = null;
  81. $this->delNode ( $tmp ) }
  82. $tmp->pre-> ;next = $tmp->next;
  83. $tmp->next->pre = $tmp->delNode ( $tmp ) }
  84. /**
  85. * @ desc: 指定されたキー値の前にノードを挿入します
  86. *
  87. * @param : $key
  88. * //指定された位置のリンク リスト要素キー
  89. * @param : $data
  90. * //リンク リスト要素挿入するデータ
  91. * @param: $flag
  92. * //位置を順番に検索して挿入するかどうか
  93. */
  94. public function insert($key, $data, $flag = true) {
  95. $newNode = self::_getNode ( $key, $data );
  96. $tmp = $this->find ( $key, $フラグ );
  97. if ($tmp !== null) {
  98. $newNode->pre = $tmp->pre;
  99. $tmp->pre = $newNode; ;
  100. $newNode->前>次 = $newNode;else {
  101. $newNode->pre = $this->tail;
  102. $this->tail->next = $newNode;
  103. $this->tail = $newNode;
  104. }
  105. $this->len ++;
  106. }
  107. /**
  108. * @ desc: 指定した位置の前にノードを挿入します
  109. *
  110. * @param : $pos
  111. * リンクリストに挿入する位置を指定します
  112. * @param : $key
  113. * 位置にあるリンクリスト要素のキー指定位置
  114. * @param : $data
  115. *挿入されるリンクリスト要素データ
  116. */
  117. public function insertPosition($pos, $key, $data) {
  118. $newNode = self::_getNode ( $key, $data );
  119. $tmp = $this->findPosition ( $pos );
  120. if ($tmp !== null) {
  121. $newNode->pre = $tmp->pre;
  122. $newNode->next = $tmp;
  123. $tmp->pre = $newNode;
  124. $newNode->pre->next = $newNode;
  125. } else {
  126. $newNode->pre = $this->tail;
  127. $this->tail->next = $newNode;
  128. $this->tail = $newNode;
  129. }
  130. $this->len ++;
  131. true を返します。
  132. }
  133. /**
  134. * @desc: キー値に基づいて指定された位置データをクエリします
  135. *
  136. * @param : $key
  137. * //指定された位置のリンク リスト要素キー
  138. * @param : $flag
  139. * //順番に検索してください
  140. */
  141. public function find($key, $flag = true) {
  142. if ($flag) {
  143. $tmp = $this->head;
  144. while ( $tmp->next !== null ) {
  145. $tmp = $tmp->next;
  146. if ($tmp->key === $key) {
  147. return $tmp;
  148. }
  149. }
  150. } else {
  151. $tmp = $this->getTail ();
  152. while ( $tmp->pre !== null ) {
  153. if ($tmp->key === $key) {
  154. return $tmp;
  155. }
  156. $tmp = $tmp->pre;
  157. }
  158. }
  159. null を返します。
  160. }
  161. /**
  162. * @ desc: 位置に基づいて指定された位置データをクエリします
  163. *
  164. * @param: $pos
  165. * //指定された位置のリンク リスト要素キー
  166. */
  167. public function findPosition($pos) {
  168. if ($pos len)
  169. return null;
  170. if ($pos len / 2 + 1)) {
  171. $tmp = $this->head;
  172. $count = 0;
  173. while ( $tmp->next !== null ) {
  174. $tmp = $tmp->next;
  175. $count ++;
  176. if ($count === $pos) {
  177. return $tmp;
  178. }
  179. }
  180. } else {
  181. $tmp = $this->tail;
  182. $pos = $this->len - $pos + 1;
  183. $count = 1;
  184. while ( $tmp->pre !== null ) {
  185. if ($count === $pos) {
  186. return $tmp;
  187. }
  188. $tmp = $tmp->pre;
  189. $count ++;
  190. }
  191. }
  192. null を返します。
  193. }
  194. /**
  195. * @desc: リンクされたリストのヘッドノードを返します
  196. */
  197. public function getHead() {
  198. return $this->head->next;
  199. }
  200. /**
  201. * @desc: リンクされたリストの末尾ノードを返します
  202. */
  203. public function getTail() {
  204. return $this->tail;
  205. }
  206. /**
  207. * @ desc: リンクリスト内のノード数をクエリします
  208. */
  209. public function getLength() {
  210. return $this->len;
  211. }
  212. プライベート静的関数 _getNode($key, $data) {
  213. $newNode = new Node_Element ( $key, $data );
  214. if ($newNode === null) {
  215. echo "新しいノードが失敗しました!";
  216. }
  217. $newNode を返します。
  218. }
  219. プライベート関数 delNode($node) {
  220. unset ( $node );
  221. $this->len --;
  222. }
  223. }
  224. // $myList = new DoubleLinkedList ();
  225. // $myList->insert ( 1, "test1" );
  226. // $myList->insert ( 2, "test2" );
  227. // $myList->insert ( "2b", "test2-b" );
  228. // $myList->insert ( 2, "test2-c" );
  229. // $myList->insert ( 3, "test3" );
  230. // $myList->insertPosition ( 5, "t", "testt" );
  231. // $myList->readAll();
  232. // エコー「+++」;
  233. // $myList->deletePosition(0);
  234. // $myList->readAll();
  235. // 「...」をエコーし​​ます。 $myList->getLength();
  236. // var_dump ( $myList->findPosition ( 3 )->data );
  237. ?>
コードをコピー


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。