Array

  • 存储具有一对一逻辑关系数据的存储顺序结构
  • 数组最大的优点:快速查询,最好应用于索引有语义的情况。

插入元素

template<typename T>
bool Array<T>::add(const int index, const T& e)
{
if (index<0 || index>size)return false; //判断索引是否正确
if (size == capacity)resize(1.5 * capacity); //判断空间是否足够,不够扩容为当前的2倍
for (int i = size - 1; i >= index; --i) //从后向前循环
{
data[i + 1] = data[i]; //全部元素向后移一位
}
data[index] = e; //插入元素
++size; //数组元素的个数+1
return true;
}

扩容与缩减

template<typename T>
void Array<T>::resize(const int newcapacity)
{
T* newdata = new T[newcapacity]; //创建新的传入大小数组空间
for (int i = 0; i < size; ++i) //把原本的数组元素放入新的数组空间
{
newdata[i] = data[i];
}
delete[] data; //释放原数组的空间
data = newdata; //把原本的数组指向新的数组
newData = nullptr;
capacity = newcapacity; //新的空间大小给原本的空间大小
}

删除元素

template<typename T>
T Array<T>::remove(const int index)
{
if (index<0 || index>size)return -1; //判断索引是否正确
T ret = data[index]; //保存临时元素
for (int i = index + 1; i < size; ++i)
{
data[i - 1] = data[i]; //从要插入的元素后一位所有元素向前移一位
}
--size; //元素个数-1
if (size == capacity / 4 && capacity / 2 != 0) //判断元素个数是否为数组容量的1/4并且数组空间不为0
{
resize(capacity / 2); //调用resize传入当前容量的一半
}
return ret; //返回被删除的元素
}

时间复杂度

均摊复杂度





  9次addLast操作,触发resize,总共进行了17次基本操作,平均每次addLast操作,进行2次基本操作。

  假设capacity = n,n+1次addLast,触发resize,总共进行2n+1次基本操作,平均每次addLast操作进行2次基本操作。

  这样均摊计算时间复杂度是O(1),同理removeLast操作,均摊复杂度也为O(1)。

代码清单

Array.h
#pragma once
#include<iostream>
template<typename T>
class Array
{
public:
//无参构造
Array() :size(0), capacity(10)
{
data = new T[10]; //默认初始化10个空间
}
//有参构造
Array(int capacity) :size(0), capacity(capacity)
{
data = new T[capacity];
}
//返回元素个数
int getSize()const;
//返回数组容量
int getCapacity()const;
//返回第一个元素
T getFirst();
//返回最后一个元素
T getLast();
//判断是否为空
bool isEmpty()const;
//插入元素
bool add(const int index, const T& e);
//头插
bool addFirst(const T& e);
//尾插
bool addLast(const T& e);
//查找数组中是否存在该元素
bool contains(const T& e)const;
//查找数组中元素的索引,不存在返回-1
int find(const T& e)const;
//删除索引元素,返回删除元素
T remove(const int index);
//删除头,返回头
T removeFirst();
//删除尾,返回尾
T removeLast();
//设置索引值
bool set(const int index, const T& e);
//获取索引值
T get(const int index)const;
//打印元素
void print()const;
//释放空间
~Array(); public:
void swap(const int i, const int j) {
if (i < 0 || i >= size || j < 0 || j >= size) {
throw Range();
}
T t = data[i];
data[i] = data[j];
data[j] = t;
} Array(T arr[], const int n) : size(n), capacity(n) {
data = new T[n];
for (int i = 0; i < n; ++i) {
data[i] = arr[i];
}
} private:
void resize(const int newcapacity); //重新分配空间
T* data;
int size; //数组大小
int capacity; //数组容量
}; template<typename T>
int Array<T>::getSize() const
{
return size; //返回数组长度
} template<typename T>
int Array<T>::getCapacity() const
{
return capacity; //返回数组容量
} template<typename T>
inline T Array<T>::getFirst()
{
return get(0);
} template<typename T>
inline T Array<T>::getLast()
{
return get(size - 1);
} template<typename T>
bool Array<T>::isEmpty() const
{
return size == 0; //判断是否为空,
} template<typename T>
bool Array<T>::add(const int index, const T& e)
{
if (index<0 || index>size)return false; //判断索引是否正确
if (size == capacity)resize(2 * capacity); //判断空间是否足够,不够扩容为当前的2倍
for (int i = size - 1; i >= index; --i) //从后向前循环
{
data[i + 1] = data[i]; //全部元素向后移一位
}
data[index] = e; //插入元素
++size; //数组元素的个数+1
return true;
} template<typename T>
bool Array<T>::addFirst(const T& e)
{
return add(0, e); //调用add从0号索引元素插入
} template<typename T>
bool Array<T>::addLast(const T& e)
{
return add(size, e); //调用add从size号索引元素插入
} template<typename T>
bool Array<T>::contains(const T& e) const
{
for (int i = 0; i < size; ++i) //遍历是否有查找的元素
{
if (data[i] == e)return true;
}
return false;
} template<typename T>
int Array<T>::find(const T& e) const
{
for (int i = 0; i < size; ++i) //遍历是否有查找的元素,有返回索引,无返回-1
{
if (data[i] == e)return i;
}
return -1;
} template<typename T>
T Array<T>::remove(const int index)
{
if (index<0 || index>size)return -1; //判断索引是否正确
T ret = data[index]; //保存临时元素
for (int i = index + 1; i < size; ++i)
{
data[i - 1] = data[i]; //从要插入的元素后一位所有元素向前移一位
}
--size; //元素个数-1
if (size == capacity / 4 && capacity / 2 != 0) //判断元素个数是否为数组容量的1/4并且数组空间不为0
{
resize(capacity / 2); //调用resize传入当前容量的一半
}
return ret; //返回被删除的元素
} template<typename T>
T Array<T>::removeFirst()
{
return remove(0); //删除0号索引元素
} template<typename T>
T Array<T>::removeLast()
{
return remove(size - 1); //删除size-1号索引元素
} template<typename T>
bool Array<T>::set(const int index, const T& e)
{
if (index<0 || index>size)return false; //判断索引元素是否正确
data[index] = e; //设置索引的新元素
return true;
} template<typename T>
T Array<T>::get(const int index) const
{
if (index<0 || index>size)return -1; //判断索引元素是否正确
return data[index]; //返回索引元素
} template<typename T>
void Array<T>::print() const
{
std::cout << "Array:size = " << size << " Array:capacity = " << capacity << std::endl;
std::cout << "Array:data = " << "[";
for (int i = 0; i < size; ++i)
{
std::cout << data[i] << ",";
}
std::cout << "]" << std::endl;
} template<typename T>
Array<T>::~Array()
{
delete[] data; //释放空间
data = nullptr;
} template<typename T>
void Array<T>::resize(const int newcapacity)
{
T* newdata = new T[newcapacity]; //创建新的传入大小数组空间
for (int i = 0; i < size; ++i) //把原本的数组元素放入新的数组空间
{
newdata[i] = data[i];
}
delete[] data; //释放原数组的空间
data = newdata; //把原本的数组指向新的数组
newData = nullptr;
capacity = newcapacity; //新的空间大小给原本的空间大小
}
main.cpp
#include"Array.h"
using namespace std; int main()
{
Array<int>* arr = new Array<int>(10);
for (int i = 0; i < 10; ++i)
{
arr->addLast(i);
}
arr->print();
cout << endl;
cout << "arr->add(1, 1)" << arr->add(1, 1) << endl;
arr->print();
cout << endl;
cout << "arr->addLast(0) " << arr->addLast(0) << endl;
arr->print();
cout << endl;
cout << "arr->addFirst(0)" << arr->addFirst(0) << endl;
arr->print();
cout << endl;
cout << "arr->contains(0)" << arr->contains(0) << endl;
arr->print();
cout << endl;
cout << "arr->find(0)" << arr->find(0) << endl;
arr->print();
cout << endl;
cout << "arr->get(1)" << arr->get(1) << endl;
arr->print();
cout << endl;
cout << "arr->set(0, 5)" << arr->set(0, 5) << endl;
arr->print();
cout << endl;
cout << "arr->getCapacity()" << arr->getCapacity() << endl;
arr->print();
cout << endl;
cout << "arr->getSize()" << arr->getSize() << endl;
arr->print();
cout << endl;
cout << "arr->isEmpty()" << arr->isEmpty() << endl;
arr->print();
cout << endl;
cout << "arr->remove(1)" << arr->remove(1) << endl;
arr->print();
cout << endl;
cout << "arr->removeFirst()" << arr->removeFirst() << endl;
arr->print();
cout << endl;
cout << "arr->removeLast()" << arr->removeLast() << endl;
arr->print();
cout << endl;
cout << "arr->getFirst" << arr->getFirst() << endl;
arr->print();
cout << endl;
cout << "arr->getLast" << arr->getLast() << endl;
arr->print(); delete arr;
return 0;
}

动态数组(Array)的更多相关文章

  1. python数据结构之动态数组

    数组列表:动态数组(Array List) 简介: 最基础简单的数据结构.最大的优点就是支持随机访问(O(1)),但是增加和删除操作效率就低一些(平均时间复杂度O(n)) 动态数组也称数组列表,在py ...

  2. array of TVarRec 动态数组使用

    FDQuery.AppendRecord()里是一个array of TVarRec.我们一般都是直接用[Var1,Var2,...].这样手工输入,但如果增加的元素我们预先不知道,就要声明一个arr ...

  3. 封装动态数组类Array

    功能: 1.增.删.改.查 2.扩容.缩容 3.复杂度分析 4.均摊复杂度 5.复杂度震荡 分析动态数组的时间复杂度: 分析resize的时间复杂度: public class Array<E& ...

  4. DelphiXe 中静态数组TByteArray和动态数组TBytes /array of byte 的区别

    在应用中发现静态数组和动态数组是有区别的: procedure TForm1.Button1Click(Sender: TObject);var  RsltStream: TMemoryStream; ...

  5. 常用数据结构-线性表及Java 动态数组 深究

    [Java心得总结六]Java容器中——Collection在前面自己总结的一篇博文中对Collection的框架结构做了整理,这里深究一下Java中list的实现方式 1.动态数组 In compu ...

  6. Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别

    ArrayList和Vector的区别ArrayList与Vector主要从二方面来说.  一.同步性:   Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...

  7. javascript类型系统——数组array

    × 目录 [1]创建 [2]本质 [3]稀疏[4]长度[5]遍历[6]类数组 前面的话 除了对象之外,数组Array类型可能是javascript中最常用的类型了.而且,javascript中的数组与 ...

  8. C#中数组Array、ArrayList、泛型List<T>的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  9. delphi动态数组指针问题

    就一个button事件 procedure TForm1.btn7Click(Sender: TObject); Type TMyArr = array of array of array of In ...

  10. JS 索引数组、关联数组和静态数组、动态数组

    JS 索引数组.关联数组和静态数组.动态数组 数组分类: 1.从数组的下标分为索引数组.关联数组 var ary1 = [1,3,5,8]; //按索引去取数组元素,从0开始(当然某些语言实现从1开始 ...

随机推荐

  1. B - Bracket Sequence题解

    B - Bracket Sequence 思路: 用一个flag来标记括号的数目,如果括号数目是个偶数的话,就代表当前要执行'+'操作,反之就是'*'操作.对于最外层的数,是没有计算的. 所以最后要单 ...

  2. python第二章pta习题总结

    chapter2 一.选择填空判断部分 1. C语言中,变量变的是内存空间中的值,不变的是地址: Python中,变量变的是地址,不变的是内存空间中的值. 2. 3.bool('False') 的返回 ...

  3. 硬件开发笔记(二):硬件开发基本流程,制作一个USB转RS232的模块(一):开发基本过程和元器件选型

    前言   做个usb转串口,同时兼容ttl,讲述硬件模块基础的开发流程,本篇描述了全流程过程,然后选型了合适的元器件.   基本流程   以下是笔者个人从事过相关硬件研发,总结出来的流程,仅代表个人意 ...

  4. 【进阶篇】使用 Redis 实现分布式缓存的全过程思考(一)

    目录 前言 一.关于缓存 二.基本数据结构 三.缓存注解 3.1自定义注解 3.2定义切点(拦截器) 3.3 AOP 实现 3.4使用示例 四.数据一致性 4.1缓存更新策略 4.2缓存读写过程 五. ...

  5. 【Azure API 管理】讨论APIM是否适合直接存储文件到Azure Storage Account呢?

    问题描述 需要从 APIM 直接将大文件存入Azure Storage,没看到这方面的官方文档,所以是否有这方面的实践案例,或者APIM是否支持引用SDK (如Azure Storage SDK)? ...

  6. 【Azure 环境】ADAL(Azure Active Directory Authentication Library )迁移到MSAL(Microsoft Authentication Library)相关问题

    问题一:根据微软官方网站对ADAL(包含ADAL.js, ADAL.NET, ADAL4J)的声明 https://docs.microsoft.com/zh-cn/azure/active-dire ...

  7. 使用 MyBatis 操作 Nebula Graph 的实践

    本文首发于 Nebula Graph Community 公众号 我最近注意到很多同学对于 ORM 框架的需求比较迫切,而且有热心的同学已经捐赠了自己开发的项目,Nebula 社区也在 working ...

  8. NET项目&DLL反编译&MSSQL监控&VS搜索&注入&上传

    知识点 1.NET普通源码&编译源码 2.DLL反编译&后缀文件&指向 3.代码审计-SQL注入&文件上传 ASPX文件 -> CS ASPX.CS DLL反编译 ...

  9. kotlin协程异常处理之-try catch

    kotlin协程小记 协程的async使用 kotlin协程异常处理之-try catch kotlin协程异常处理之-CoroutineExceptionHandler 一.try catch tr ...

  10. 聊聊微信小程序的流式(stream)响应请求

    场景:类似ChatGPT的逐字显示效果. 流程:服务端我用Python的flask框架(向外提供API接口)实现,服务部署在replit上,Python调用azure 的chatgpt服务(需要申请) ...