1. 以PHP的方式学习数据结构 -- 数组2020-10-21 11:51:45
说明:代码使用了composer类的自动加载,并采用psr4命名规范,如不使用可需要namespace的定义1. 实现逻辑<?php
/**
* content: 数组的实现
* create: 2020-10-20
*/
namespace ArrayBundle;
class BaseArray
{
/**
* 数组当前长度
* @var int
*/
protected $size;
/**
* 数组的总容量
* @var int
*/
protected $capacity;
/**
* 数组的内容
* @var array
*/
protected $data;
/**
* 初始化数组内容
* BaseArray constructor.
* @param int $capacity
*/
public function __construct(int $capacity = 100)
{
if ($capacity <= 0) exit('初始化数组失败!数组总长度必须大于0!');
$this->capacity = $capacity;
$this->size = 0;
$this->data = [];
}
/**
* 打印内容
* @return string
*/
public function __toString()
{
$result = sprintf('Array: size = %d, capacity = %d'. PHP_EOL, $this->size, $this->capacity);
$result .= '[';
for ($i = 0; $i < $this->size; $i++) {
$result .= $this->data[$i];
if ($i != $this->size - 1) {
$result .= ',';
}
}
$result .= ']';
return $result;
}
/**
* 判断数组是否为空
* @return bool
*/
public function isEmpty(): bool
{
return $this->size == 0;
}
/**
* 判断数组是否已经存满
* @return bool
*/
public function isFull(): bool
{
return $this->size == $this->capacity;
}
/**
* 调整数组的容量大小
* @param float $ratio 小于1为缩小,大于1为扩大
*/
public function resize(float $ratio): void
{
if ($ratio > 1) {
if ($this->size < $this->capacity) return;
} elseif ($ratio < 1) {
if ($this->size > $this->capacity / 2) return;
} else {
return;
}
$this->capacity = $this->capacity * $ratio;
}
/**
* 插入元素
* @param int $index 插入的索引
* @param mixed $value 插入的值
* @return void
*/
public function add(int $index, $value): void
{
// 如果当前的索引 大于 最大的容量值就返回
if ($index > $this->capacity) return;
// 如果数组存满了就扩容
if ($this->isFull()) {
$this->resize(2);
}
for ($i = $this->size - 1; $i >= $index; $i--) {
$this->data[$i + 1] = $this->data[$i];
}
// 插入数组
$this->data[$index] = $value;
// 当前长度自增
$this->size++;
return;
}
/**
* 在头部插入元素
* @param mixed $value
* @return void
*/
public function addFirst($value): void
{
$this->add(0, $value);
}
/**
* 在末尾插入元素
* @param mixed $value
* @return void
*/
public function addLast($value): void
{
$this->add($this->size, $value);
}
/**
* 删除元素
* @param int $index 删除的索引
* @return bool
*/
public function del(int $index): bool
{
// 判断是否为空 或 索引是否在当前长度内
if ($this->isEmpty() || $this->size <= $index) return false;
// 将值往前推进一位, 初始值为需要删除的下标索引,直到最后
for ($i = $index; $i < $this->size - 1; $i++) {
$this->data[$i] = $this->data[$i + 1];
} // 将最后一位的值设为null
$this->data[$this->size - 1] = null;
$this->size--;
$this->resize(0.5);
return true;
}
/**
* 更新指定字段
* @param int $index 索引下标
* @param mixed $value
* @return bool
*/
public function update(int $index, $value): bool
{
// 判断是否为空 或 索引是否在当前长度内
if ($this->isEmpty() || $this->size <= $index) return false;
if ($this->search($value) == -1) return false;
$this->data[$index] = $value;
return true;
}
/**
* 替换值
* @param mixed $searchValue 查询的值
* @param mixed $newValue 新的值
*/
public function replace($searchValue, $newValue): void
{
for ($i = 0; $i < $this->size; $i++) {
if ($this->data[$i] == $searchValue) {
$this->data[$i] = $newValue;
}
}
}
/**
* 根据值查询搜因
* @param $value
* @return int
*/
public function search($value): int
{
if ($this->isEmpty()) return -1;
for ($i = 0; $i < $this->size; $i++) {
if ($this->data[$i] == $value) return $i;
}
return -1;
}
}2. 执行逻辑<?php
require_once __DIR__. '/../vendor/autoload.php';
$array = new ArrayBundleBaseArray(1);
// 从末尾插入5个元素
$array->addLast('a');
$array->addLast('b');
$array->addLast('c');
$array->addLast('d');
$array->addLast('e');
// 从开头插入3个元素
$array->addFirst('z');
$array->addFirst('z');
$array->addFirst('z');
// 删除前面3个元素
$array->del(0);
$array->del(0);
$array->del(0);
// 打印结果
echo $array;3. 打印结果Array: size = 5, capacity = 8
[a,b,c,d,e]
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。
Tags 标签php数据结构数组
扩展阅读