PHP实现双向链表

Tags: ,
Comments: No Comments
Published on: 2012 年 10 月 30 日

由于需要对一组数据多次进行移动操作,所以写个双向链表。但对php实在不熟悉,虽然测试各个方法没啥问题,就是不知道php语言深层的这些指针和unset有什么注意的地方,贴出来让大家教育吧。效率没测试....求谅解~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
 * **双向链表
 * @author zhiyuan12@
 * @modified 2012-10-25
 */
 
/**
 * 链表元素结点类
 */
class Node_Element {
	public $pre = NULL; // 前驱
	public $next = NULL; // 后继
	public $key = NULL; // 元素键值
	public $data = NULL; // 结点值
	function __Construct($key, $data) {
		$this->key = $key;
		$this->data = $data;
	}
}
 
/**
 * 双向链表类
 */
class DoubleLinkedList {
	private $head; // 头指针
	private $tail; // 尾指针
	private $current; // 当前指针
	private $len; // 链表长度
	function __Construct() {
		$this->head = self::_getNode ( null, null );
		$this->curelement = $this->head;
		$this->tail = $this->head;
		$len = 0;
	}
 
	/**
	 * @ desc: 读取链表全部结点
	 */
	public function readAll() {
		$tmp = $this->head;
		while ( $tmp->next !== null ) {
			$tmp = $tmp->next;
			var_dump ( $tmp->key, $tmp->data );
		}
	}
	public function move($pos1, $pos2) {
		$pos1Node = $this->findPosition ( $pos1 );
		$pos2Node = $this->findPosition ( $pos2 );
		if ($pos1Node !== null && $pos2Node !== null) {
			$tmpKey = $pos1Node->key;
			$tmpData = $pos1Node->data;
			$pos1Node->key = $pos2Node->key;
			$pos1Node->data = $pos2Node->data;
			$pos2Node->key = $tmpKey;
			$pos2Node->data = $tmpData;
			return true;
		}
		return false;
	}
 
	/**
	 * @ desc: 在指定关键词删除结点
	 *
	 * @param : $key
	 *        	指定位置的链表元素key
	 */
	public function delete($key) {
		$pos = $this->find ( $key );
		if ($pos !== null) {
			$tmp = $pos;
			$last = null;
			$first = true;
			while ( $tmp->next !== null && $tmp->next->key === $key ) {
				$tmp = $tmp->next;
				if (! $first) {
					$this->delNode ( $last );
				} else {
					$first = false;
				}
				$last = $tmp;
			}
 
			if ($tmp->next !== null) {
				$pos->pre->next = $tmp->next;
				$tmp->next->pre = $pos->pre;
			} else {
				$pos->pre->next = null;
			}
			$this->delNode ( $pos );
			$this->delNode ( $tmp );
		}
	}
	/**
	 * @ desc: 在指定位置删除结点
	 *
	 * @param : $key
	 *        	指定位置的链表元素key
	 */
	public function deletePosition($pos) {
		$tmp = $this->findPosition ( $pos );
		if ($tmp === null) {
			return true;
		}
		if ($tmp === $this->getTail ()) {
			$tmp->pre->next = null;
			$this->delNode ( $tmp );
			return true;
		}
		$tmp->pre->next = $tmp->next;
		$tmp->next->pre = $tmp->pre;
		$this->delNode ( $tmp );
	}
 
	/**
	 * @ desc: 在指定键值之前插入结点
	 *
	 * @param : $key
	 *        	//指定位置的链表元素key
	 * @param : $data
	 *        	//要插入的链表元素数据
	 * @param : $flag
	 *        	//是否顺序查找位置进行插入
	 */
	public function insert($key, $data, $flag = true) {
		$newNode = self::_getNode ( $key, $data );
		$tmp = $this->find ( $key, $flag );
		if ($tmp !== null) {
			$newNode->pre = $tmp->pre;
			$newNode->next = $tmp;
 
			$tmp->pre = $newNode;
			$newNode->pre->next = $newNode;
		} else {
			$newNode->pre = $this->tail;
			$this->tail->next = $newNode;
			$this->tail = $newNode;
		}
		$this->len ++;
	}
	/**
	 * @ desc: 在指定位置之前插入结点
	 *
	 * @param : $pos
	 *        	指定插入链表的位置
	 * @param : $key
	 *        	指定位置的链表元素key
	 * @param : $data
	 *        	要插入的链表元素数据
	 */
	public function insertPosition($pos, $key, $data) {
		$newNode = self::_getNode ( $key, $data );
		$tmp = $this->findPosition ( $pos );
		if ($tmp !== null) {
			$newNode->pre = $tmp->pre;
			$newNode->next = $tmp;
 
			$tmp->pre = $newNode;
			$newNode->pre->next = $newNode;
		} else {
			$newNode->pre = $this->tail;
			$this->tail->next = $newNode;
			$this->tail = $newNode;
		}
		$this->len ++;
		return true;
	}
	/**
	 * @ desc: 根据key值查询指定位置数据
	 *
	 * @param : $key
	 *        	//指定位置的链表元素key
	 * @param : $flag
	 *        	//是否顺序查找
	 */
	public function find($key, $flag = true) {
		if ($flag) {
			$tmp = $this->head;
			while ( $tmp->next !== null ) {
				$tmp = $tmp->next;
				if ($tmp->key === $key) {
					return $tmp;
				}
			}
		} else {
			$tmp = $this->getTail ();
			while ( $tmp->pre !== null ) {
				if ($tmp->key === $key) {
					return $tmp;
				}
				$tmp = $tmp->pre;
			}
		}
 
		return null;
	}
	/**
	 * @ desc: 根据位置查询指定位置数据
	 *
	 * @param : $pos
	 *        	//指定位置的链表元素key
	 */
	public function findPosition($pos) {
		if ($pos <= 0 || $pos > $this->len)
			return null;
		if ($pos < ($this->len / 2 + 1)) {
			$tmp = $this->head;
			$count = 0;
			while ( $tmp->next !== null ) {
				$tmp = $tmp->next;
				$count ++;
				if ($count === $pos) {
					return $tmp;
				}
			}
		} else {
			$tmp = $this->tail;
			$pos = $this->len - $pos + 1;
			$count = 1;
			while ( $tmp->pre !== null ) {
				if ($count === $pos) {
					return $tmp;
				}
				$tmp = $tmp->pre;
				$count ++;
			}
		}
		return null;
	}
	/**
	 * @ desc: 返回链表头节点
	 */
	public function getHead() {
		return $this->head->next;
	}
	/**
	 * @ desc: 返回链表尾节点
	 */
	public function getTail() {
		return $this->tail;
	}
	/**
	 * @ desc: 查询链表节点个数
	 */
	public function getLength() {
		return $this->len;
	}
	private static function _getNode($key, $data) {
		$newNode = new Node_Element ( $key, $data );
		if ($newNode === null) {
			echo "new node fail!";
		}
		return $newNode;
	}
	private function delNode($node) {
		unset ( $node );
		$this->len --;
	}
}
 
// $myList = new DoubleLinkedList ();
// $myList->insert ( 1, "test1" );
// $myList->insert ( 2, "test2" );
// $myList->insert ( "2b", "test2-b" );
// $myList->insert ( 2, "test2-c" );
// $myList->insert ( 3, "test3" );
// $myList->insertPosition ( 5, "t", "testt" );
// $myList->readAll ();
// echo "+++";
// $myList->deletePosition(0);
// $myList->readAll ();
// echo "..." . $myList->getLength ();
// var_dump ( $myList->findPosition ( 3 )->data );
?>

我猜你可能也喜欢:

No Comments - Leave a comment

Leave a comment

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is 星期五, 2017 年 09 月 22 日