php中,是实现顺序链表,
顺序链表的创建,需要先声明个数 。
如果超过个数后,再次插入数据,会 超出范围异常
此循序表 ,使用 splFiexArray 结构实现
<?php
/**
* Created by PhpStorm.
* User: songyongzhan
* Date: 2018/11/30
* Time: 11:02
* Email: songyongzhan@qianbao.com
*/
class SeqList {
const MAX = 80;
private $max;
private $num;
/**
* @var SplFixedArray
*/
private $item;
public function __construct($max = self::MAX) {
$this->max = $max;
$this->num = 0;
$this->item = new SplFixedArray($this->max);
}
public function isEmpty() {
return empty($this->num);
}
/**
* 目前插入了多少数据
* @return int
*/
public function count() {
return $this->num;
}
/**
* 在尾部添加value值,根据顺序表添加
* @param $value
*/
public function append($value) {
if ($this->isFull()) {
printf("存储表已满" . PHP_EOL);
return;
}
$this->item[$this->num] = $value;
$this->num++;
}
/**
* 判断这个元素是否存在
* @param $index
* @return bool
*/
public function isExists($index) {
return $this->item->offsetExists($index);
}
/**
*
*/
public function clear() {
$this->__construct();
}
/**
* @param $index
* @param $val
* @throws Exception
*/
public function add($index, $val) {
if (!is_numeric($index))
throw new Exception('$index must be numeric');
if ($index < 0 || $index > $this->max)
throw new RuntimeException('$index 不能超过最大的值' . $this->max . ' 或不能小于0');
for ($i = $this->num; $i > $index; $i--) {
$this->item[$i] = $this->item[$i - 1];
}
$this->item[$index] = $val;
$this->num++;
}
/**
* @param $index
* @throws Exception
*/
public function delItem($index) {
try {
if (!is_numeric($index))
throw new Exception($index);
if ($index < 0 || $index >= $this->num)
throw new RuntimeException($index);
} catch (TypeError $e) {
echo $e->getMessage();
} catch (RuntimeException $e) {
echo $e->getMessage();
}
for ($i = $index; $i < ($this->num - 1); $i++) {
$this->item[$i] = $this->item[$i + 1];
}
$this->num--;
}
public function setItem($index, $val) {
if (!is_numeric($index))
throw new Exception($index);
if ($index < 0 || $index >= $this->num)
throw new RuntimeException($index);
if ($index >= 0 && $index < $this->num) {
$this->item[$index] = $val;
}
return TRUE;
}
public function getLocation($val) {
for ($i = 0; $i < $this->num; $i++) {
if ($this->item[$i] == $val)
return $i;
}
return -1;
}
public function isFull() {
if ($this->num >= $this->max)
return TRUE;
else return FALSE;
}
public function getList() {
/* $data = [];
for ($i = 0; $i < $this->num; $i++) {
$data[$i] = $this->item[$i];
}
return $data;*/
return $this->item->toArray();
}
public function getItem() {
return $this->item;
}
/**
*
* @return int
*/
public function getFiexArraySize() {
return $this->item->getSize();
}
/**
* @param $num
* @return bool
* @throws Exception
*/
public function setFiexArraySize($num) {
if (!is_numeric($num))
throw new Exception('设置FiexArray值必须是数字');
if ($num < 0)
throw new RuntimeException('设置FiexArray值长度必须大于0');
$this->max = $num;
return $this->item->setSize($num);
}
}
$seq = new SeqList(5);
$seq->setFiexArraySize(12);
$seq->append(1);
$seq->append(2);
$seq->append(3);
//$seq->append(4);
//$seq->append(5);
//$seq->append(6);
//
$seq->add(9, 10000);
//$seq->append(7);
$seq->append(8);
//$seq->append(9);
//$seq->append(10);
printf("index 9 是否存在:%s\n",$seq->isExists(9));
var_dump($seq->count());
//$seq->delItem(2);
//$seq->delItem(4);
$seq->add(2, 10);
// print_r($seq->clear());
print_r($seq->getList());
var_dump($seq->getFiexArraySize());
$sqlFixedArr = $seq->getItem();
var_dump($sqlFixedArr->getSize());
var_dump($seq->getLocation(9));