自己写的链表,测试通过。但不保证在生产环境下能正常使用
<?php
/********************************************************
* desc: 双链表
* author: masin
* version: $1.0
* date: 2008-12-29
******************************************************/
/**
* 链表元素结构部分定义
**/
class List_Element
{
public $pre = NULL;
public $next = NULL;
public $key = NULL;
public $data = NULL;
function __Construct($key, $data)
{
$this->key = $key;
$this->data = $data;
}
}
/**
* 链表结构的具体实现部分
**/
class FList
{
private $head; //头指针
private $tail; //尾指针
private $curelement; //当前指针
private $flen; //链表长度
private $maxelement;
function __Construct($maxrow)
{
$this->head = NULL;
$this->tail = NULL;
$this->curelement = NULL;
$this->links = array();
$this->maxelement = $maxrow;
}
/**
* @ desc: 在链表未端添加链表元素
* @param: int $key //指定位置的链表元素key
* @param: string $data //要插入的链表元素数据
**/
function append($key, $data)
{
$newelement = $this->_getElement($key, $data);
if($this->_isEmpty())
{
$this->head = $newelement;
$this->tail= $newelement;
}
else
{
$newelement->pre = $this->curelement;
$newelement->next = $this->curelement->next;
$this->curelement->next = $newelement;
$tmp->next->pre = $newelement;
$this->tail = $newelement;
}
$this->flen += 1;
$this->curelement = $newelement;
}
/**
* @ desc: 在指定的位置前插入链表元素
* @param: int $key //指定位置的链表元素key
* @param: List_Element $newelement //要插入的链表元素
**/
function insertEleBefore($key, $newelement)
{
//$newelement = $this->_getElement($newkey, $newdata);
$tmp = $this->find($key);
if($tmp === NULL)
{
$this->head = $newelement;
$this->tail = $newelement;
}
else if($tmp === $this->head)
{
$this->head = $newelement;
$newelement->pre = $tmp->pre;
$newelement->next = $tmp;
$tmp->pre->next = $newelement;
$tmp->pre = $newelement;
}
else
{
$newelement->pre = $tmp->pre;
$newelement->next = $tmp;
$tmp->pre->next = $newelement;
$tmp->pre = $newelement;
}
$this->flen += 1;
$this->curelement = $newelement;
}
/**
* @ desc: 删除链表中指定的元素
* @param: List_Element $element //要删除的链表元素
*/
function delElement($element)
{
if($this->head === NULL)
{
printf(" it is empty linkedlist");
exit;
}
if($element === NULL)
{
printf("no matched element in the linkedlist");
exit;
}
$this->curelement = $element->next;
$element->pre->next = $this->curelement;
$this->curelement->pre = $element->pre;
unset($element);
}
/**
* @desc: 删除所有链表元素
**/
function delAll()
{
$this->curelement = $this->head;
$tmp = NULL;
while($this->curelement->next != NULL)
{
$tmp = $this->curelement;
$this->curelement = $this->curelement->next;
unset($tmp);
}
}
/**
* @ desc: 移动链表中的元素
* @param: int $skey //要移动的链表元素key
* @param: int $dkey //目标位置链表元素key
*/
function moveElement($skey, $dkey)
{
$tmp = $this->find($skey);
$tmp->pre->next = $tmp->next;
$tmp->next->pre = $tmp->pre;
$this->insertEleBefore($dkey, $tmp);
}
/**
* @ desc: 输出链表的内容
* @param: int $pos //链表开始位置
* @param: int $num //输出链表元数个数
*
* @return array $tmplinks //将链表元数以数组形式输出
*/
function display($pos, $num)
{
$tmp = NULL;
if($this->_isEmpty())
{
printf("it is empty linkedlist");
exit;
}
$this->move($pos);
$tmp = $this->curelement;
$tmplinks = array();
$getnum = 0;
if(($pos + $num) > $this->flen)
{
$num = $this->flen - $pos;
}
while($getnum < $num)
{
$tmplinks[] = $tmp;
$tmp = $tmp->next;
$getnum++;
}
return $tmplinks;
}
/**
* @ desc: 移动链表指针到指定的位置
* @param: int $pos //指定的链表位置
*/
function move($pos)
{
if($pos > $this->flen)
{
$this->curelement = $this->tail;
return true;
}
$tmp = $this->head;
$curpos = 0;
while($tmp->next != null && $curpos < $pos)
{
$tmp = $tmp->next;
$curpos++;
}
$this->curelement = $tmp;
return true;
}
/**
* @ desc: 按元素关键字查找链表元素所在的位置
* @param: int $key //链表元素关键字
*/
function find($key)
{
$tmp = $this->head;
$dest = NULL;
while($tmp->next !== NULL )
{
if($tmp->key === $key)
{
$dest = $tmp;
break;
}
else
{
$tmp = $tmp->next;
}
}
return $dest;
}
/**
* @ desc: 生成一个链表元素
* @param: int $key //链表元素关键字
* @param: string $value //链表元素数据
**/
function _getElement($key, $value)
{
$tmpnode = new List_Element($key, $value);
return $tmpnode;
}
/**
* @desc: 判断链表是否为空
*/
function _isEmpty()
{
return $this->head === NULL;
}
}
?>
用php实现的双链表
最新推荐文章于 2021-12-24 12:14:10 发布