php 线性表,线性表的php实现

实例

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);

}

}

运行实例 »

点击 "运行实例" 按钮查看在线实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值