php 数据结构set,数据结构之php实现数组

说到数组,几乎每个IT江湖人士都不陌生,甚至过半人还会很自信觉得它很简单。的确,在大多数编程语言中几乎都会有数组的影子。不过它不仅仅是一种基础的数据类型,更是一种基础的数据结构。

一、概念

那什么是数组呢?  我们一起来看下定义:在计算机科学中,数组数据结构(英语:array data structure),简称数组(英语:Array),是由相同类型的元素(element)的集合所组成的数据结构,分配一块连续的内存来存储维基百科

正如以上所述, 数组在应用上属于数据的一个集合, 就是一个容器。不过我需要补充一点的是, 数组在数据结构范畴属于一种线性数据结构, 也就是只有前置节点和后续节点的数据结构。除了数组, 像平时所用的队列,栈,链表等也都是属于线性结构。

ce0ac6650eda9e4e6b6e2b127159f21a.png

数组存储图形

相比各位IT江湖人士都知道的一件事情, 大部分的变成语言中数组的下标是从 0 开始编号的, 为什么这个与我们平时生活中从1开始编号的习惯相比有点"反人类"呢?因为,数组在内存中是连续存储的, 当数组初始化后,数组的长度固定不变;需要增加数组长度时,由于数组的存储空间附近可能被其他数据存储的空间占用, 所以只能创建一片新的存储空间用来存储数组。而获取数组元素时,获取规则为:数组下标 * 数组类型字节大小 + 数组首地址的方式进行获取。如:一个int类型(4个字节)的数组,假设首地址为“2”。那么,第一位元素的地址 = 0 * 4 + 2;第二位元素的地址 = 1 * 4 + 2。

我们列出一个公式来进行分析一下。// 下标从0开始arr[i] = i * type_bytes(4) + base_address// 下标从1开始arr[i] = (i -1) * type_bytes(4) + base_address

比较两个计算公式可以发现公式(2)每次CPU寻址需要多一次i-1的操作,即多了一次减法的指令运算。对于数组这种基础数据结构,无论在哪种高级程序语言中,都是频繁间接(作为容器的基础数据结构,比如Java的ArrayList)或者直接被使用的,因此要尽量减少其消耗CPU资源。

二、代码实现<?php

class ArrayStruct{

// 数组实际元素

private$size = 0;

// 用于存放数据

private$data = [];

// 用于标记数组的容量大小

private$capacity =10;

/**

* 构造函数 定义数组容量

* ArrayStruct constructor.

* @param int $capacity

*/public function __construct(int $capacity = 10){

$this->capacity = $capacity;

}

/**

* 获取数组元素个数

* @return int

*/

public function getSize(): int{

return $this->size;

}

/**

* 获取数组的容量

* @return int

*/

public function getCapacity(): int{

return $this->capacity;

}

/**

* 判断数组是否为空

* @return bool

*/

public function isEmpty(): bool{

return $this->size == 0;

}

/**

* 向数组指定位置插入元素

* @param int $index

* @param $e

* @throws Exception

*/

public function add(int $index, $e): void{

if ($this->size == $this->capacity) {

$this->resize(2); //扩大到原来的2倍

}

if ($index < 0 || $index > $this->size) {

echo "添加位置超出数组大小";

exit;

}

//为了方便理解,[1,2,4,5,6],假设 $index = 3; $e = 100,插入之后[1,2,4,100,5,6]

for ($i = $this->size; $i >= $index; $i--) {

$this->data[$i] = $this->data[$i - 1];

}

$this->data[$index] = $e;

$this->size++;

}

/**

* 向数组末尾添加元素

* @param $e

* @throws Exception

*/

public function addLast($e): void{

$this->add($this->size, $e);

}

/**

* 向数组开头插入元素

* @param $e

* @throws Exception

*/

public function addFirst($e): void{

$this->add(0, $e);

}

/**

* 获取 index 位置数组元素

* @param int $index

* @return mixed

*/

public function get(int $index){

if ($index < 0 || $index > $this->size) {

echo "index值超出元素的位置范围,";

exit;

}

return $this->data[$index];

}

/**

* 判断数组中是否存在某个元素

* @param $e

* @return bool

*/

public function contains($e): bool{

for ($i = 1; $i < $this->size; $i++) {

if ($this->data[$i] == $e) {

return true;

}

}

return false;

}

/**

* 查某个元素在数组的位置索引值,若不存在则返回 -1

* @param $e

* @return int

*/

public function find($e): int{

for ($i = 0; $i < $this->size; $i++) {

if ($this->data[$i] == $e) {

return $i;

}

}

return -1;

}

/**

* 删除数组指定位置元素,返回删除元素的值

* @param $index

* @return mixed

*/

public function remove($index){

if ($index < 0 || $index > $this->size) {

echo "index值超出元素的位置范围,";

exit;

}

$e = $this->data[$index];

for ($i = $index; $i < $this->size - 1; $i++) {

$this->data[$i] = $this->data[$i + 1];

}

$this->size--;

$this->data[$this->size] = null;

/** 若当前数组大小,小于容量的一半,则重新分配一半的数组空间大小 **/

if ($this->size <= $this->capacity / 4 && $this->capacity % 2 == 0) {

$this->resize(0.5);

}

return $e;

}

/**

* 删除数组首个元素,返回删除元素的值

*/

public function removeFirst(){

return $this->remove(0);

}

/**

* 删除数组首个元素,返回删除元素的值

*/

public function removeLast(){

return $this->remove($this->size);

}

/**

* 删除数组中特定元素

* @param $e

*/

public function removeElement($e){

for ($i = 0; $i < $this->size; $i++) {

if ($this->data[$i] == $e) {

$this->remove($i);

$this->removeElement($e);

break;

}

}

}

/**

* 数组扩容,若是其他语言,如JAVA这里需要重新开辟空间

* @param $factor

*/

protected function resize($factor){

$this->capacity = $factor * $this->capacity;

}

/**

* 将数组转化为字符串

* @return string

*/

public function toString(): string

{

$str = 'Array: size = ' . $this->size . ',' . 'capacity = ' . $this->capacity . PHP_EOL;$str .= "[";

for ($i = 0; $i < $this->size; $i ++) {

$str .= $this->data[$i];

if ($i != $this->size - 1) {

$str .= ',';

}

}

$str .= "]";

return $str;

}

}

$arrayStructure = new ArrayStructure(15);

$arrayStructure->add(0, 1);

$arrayStructure->add(1, 2);

$arrayStructure->add(2, 3);

echo $arrayStructure->toString();

$arrayStructure->add(2, 4);

echo $arrayStructure->toString();

$arrayStructure->addFirst(- 72);

echo $arrayStructure->toString();

echo $arrayStructure->remove(0) . PHP_EOL;

echo $arrayStructure->toString();

echo $arrayStructure->removeLast() . PHP_EOL;

echo $arrayStructure->toString();

三、复杂度简单分析O: 描述是算法的运行时间 和 输入数据之间的关系 ---  程序运行时间 和 数数据 成线性关系 O(n)

添加操作

addLast(num)         O(1)addFirst(num)        O(n)addIndex(index,num)  O(n)   严格计算需要一些概率论知识

删除操作

removeLast(num)      O(1)removeFisrt()        O(n)remove(index, num)   O(n/2) = O(n)

查找操作

getIndex(index)     O(1)contains(num)       O(n)find(num)           O(n)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值