实例
class straitList{
public $list;
/**
* 初始化.
* @param $array
*/
public function __construct($array)
{
$this->list = $array;
}
/**
* 线性表是否为空
* @return bool
*/
public function listEmpty()
{
return empty($this->list);
}
/**
* 情况线性表
*/
public function clearList()
{
unset($this->list);
}
/**
* @param $item 线性表的序号
* @return bool
*/
public function getElem($item)
{
return array_key_exists($item, $this->list) ? $this->list[$item] : false;
}
/**
* @param $v 元素值 返回元素在线性表中匹配到的第一个序号或者false
* @return bool|false|int|string
*/
public function locateElem($v)
{
$re = array_search($v, $this->list);
return $re === false ? false : $re;
}
/**
* @param $item 插入的位置
* @param $value 要插入的元素
* @return bool
*/
public function listInsert($item, $value)
{
$len = $this->listLength();
if ($item $len) {
return false;
}
for ($i = $len-1; $i >$item-1; $i--) {
$this->list[$i+1] = $this->list[$i];
}
$this->list[$item-1] = $value;
return true;
}
/**
* @param $item 要删除的元素序号
* @return bool
*/
public function ListDelete($item)
{
$len = $this->listLength();
if ($item $len) {
return false;
}
$return = $this->list[$item-1];
for ($i = $item-1; $i
$this->list[$i] = $this->list[$i+1];
}
unset($this->list[$len-1]);
return $return;
}
/**
* @return int
*/
public function listLength()
{
return count($this->list);
}
}
运行实例 »
点击 "运行实例" 按钮查看在线实例