• 展开微博窗口
  • QQ:52619941
  • 微信:cnmemory
  • 展开分类目录
  • 还没有账号?

Memory

php模拟链表

<?php
class LinkTable {

//头节点
public $head = NULL;
//连接下一个节点
public $next = NULL;
//连接上一个节点
public $prev = NULL;
//用于存放节点数据
public $object = array();
public $len = 0;

public function __construct($object = array()) {
$this->object = $object;
$this->head = & $this;
$this->len++;
}

public function put($object) {
$current = $this;
$prev = null;
do{
$prev = &$current;
$current = &$current->next;
}while($current);
$current = new self($object);
$current->prev = $prev;
$this->len++;
}

public function show() {
$str = '';
$current = $this;
while($current) {
$str .= $current->object;
$current = $current->next;
}
echo $str;
}

}

$link = new LinkTable("hello ");
$link->put("world ");
$link->put("!!!");
$a = $link->next->next->head;
echo $a->object;
$a = $a->head;
echo $link->len;
$link->show();
?>
码字很辛苦,转载请注明来自雨林寒舍《php模拟链表》

评论