zoukankan      html  css  js  c++  java
  • 数据结构之数组

    最近在重温数据结构与算法,今天就从最为简单的数组开始吧(文中代码全是C++语言实现)。

    1. 数组的特点

    • 数组是有限个相同类型的元素所组成的有序集合。
    • 数组是一种线性的物理结构,存储方式为顺序存储,访问方式是随机访问。
    • 利用下标查找元素的时间复杂度为O(1),插入、删除元素的时间复杂度为O(n)。
    • 数组适用于读操作多,写操作少的场景。

    2. 数组的基本操作

    数组的操作方式主要分为增、删、改、查,下面分别以最简单的方式介绍基本用法。

    2.1 查询

    1 int aValue[5] = {1,5,6,10,23};
    2 printf("index = 0,value = %d 
    ", aValue[0]);

    2.2 修改

    1 aValue[0] = 100;
    2 printf("aValue[0] = %d 
    ", aValue[0]);

    2.3 移除

    移除指定索引的元素本质上是将index+1至最后一个索引对应的元素全部向前移动一位。

    首先定义一个数组的数据结构,头文件:

     1 class MyArray1
     2 {
     3 public:
     4     MyArray1(int nCapcity = 16);
     5     ~MyArray1();
     6     int  Remove(int nIndex);
     7 private:
     8     //扩容
     9     void Resize();
    10 private:
    11     int m_nSize;
    12     int m_nCapcity;
    13     int* m_pArray;
    14 };

    CPP 文件:

     1 //构造函数
     2 MyArray1::MyArray1(int nCapcity)
     3     :m_pArray(nullptr)
     4     , m_nCapcity(nCapcity)
     5     , m_nSize(0)
     6 {
     7     m_pArray = new int[m_nCapcity];
     8 }
     9 
    10 //析构函数
    11 MyArray1::~MyArray1()
    12 {
    13     delete m_pArray;
    14 }
    15 
    16 //移除
    17 int MyArray1::Remove(int nIndex)
    18 {
    19     if (nullptr == m_pArray)
    20     {
    21         assert(false && "nullptr == m_pArray");
    22         return -1;
    23     }
    24     if (nIndex < 0 || nIndex >= m_nSize)
    25     {
    26         assert(false && "nIndex < 0 || nIndex >= m_nSize");
    27         return -1;
    28     }
    29 
    30     int nElement = m_pArray[nIndex];
    31     for (int i = nIndex; i < (m_nSize - 1); i++)
    32     {
    33         m_pArray[i] = m_pArray[i + 1];
    34     }
    35     m_nSize--;
    36     return nElement;
    37 }

    2.4 插入

    插入可以分为尾部新增和中间插入两种情况,中间插入元素本质是将index+1至最后一个索引对应的元素全部向后移动一位。

    不过插入元素中设计到扩容的概念,所谓扩容,就是扩大数组的长度。当插入元素时,若发现数组长度已满,此时需要扩容数组,操作为创建一个新的同类型的数组,长度为原来的2倍,将原始数组的元素拷贝到新数组中,此时即可插入新元素。

     1 void MyArray1::Insert(int nIndex, int nElement)
     2 {
     3     if (nullptr == m_pArray)
     4     {
     5         assert(false && "nullptr == m_pArray");
     6         return;
     7     }
     8     if (nIndex < 0 || nIndex > m_nSize)
     9     {
    10         assert(false && "nIndex < 0 || nIndex > m_nSize");
    11         return;
    12     }
    13 
    14     if (m_nSize >= m_nCapcity)
    15     {
    16         Resize();
    17     }
    18 
    19     for (int i = m_nSize - 1; i >= nIndex;i--)
    20     {
    21         m_pArray[i + 1] = m_pArray[i];
    22     }
    23 
    24     m_pArray[nIndex] = nElement;
    25     m_nSize++;
    26 }
    27 
    28 
    29 void MyArray1::Resize()
    30 {
    31     if (nullptr == m_pArray)
    32         return;
    33 
    34     int* pNewArray = new int[m_nCapcity * 2];
    35     memcpy(pNewArray, m_pArray, sizeof(int) * m_nSize);
    36     m_nCapcity = m_nCapcity * 2;
    37     delete m_pArray;
    38     m_pArray = pNewArray;
    39 }
     
     
  • 相关阅读:
    react路由组件&&非路由组件
    react函数式组件(非路由组件)实现路由跳转
    react使用antd组件递归实现左侧菜单导航树
    【LeetCode】65. Valid Number
    【LeetCode】66. Plus One (2 solutions)
    【LeetCode】68. Text Justification
    【LeetCode】69. Sqrt(x) (2 solutions)
    【LeetCode】72. Edit Distance
    【LeetCode】73. Set Matrix Zeroes (2 solutions)
    【LeetCode】76. Minimum Window Substring
  • 原文地址:https://www.cnblogs.com/calence/p/11406524.html
Copyright © 2011-2022 走看看