数组基础与线性表特性
数组
数组属于线性表这一类数据结构,其核心特征是利用连续的内存空间来存放类型一致的数据集合。
1.线性结构:数据元素呈现线性排列,每个节点最多仅涉及前后两个方向的关系,例如链表、队列和栈等。掌握数组的动态操作后,可轻松迁移至其他线性结构的实现。深入阅读如《STL源码剖析》等底层源码,能显著降低理解难度。 2.非线性结构:包括二叉树、堆以及图等复杂结构。 3.连续性与同构性:由于数组在内存中连续存储,执行插入或删除操作时,为维持数据的连续性,通常需进行大量元素搬移,导致效率较低。
动态数组功能实现
1.数组初始化
为防止扩容过程中因数据搬移引发内存泄露,实现中引入双缓冲机制,通过设置内存标志位 ItemsFlag 进行管理。当 ItemsFlag 为 0 时使用 preitems,为 1 时使用 items。具体逻辑见下文扩容部分。默认初始容量设为 10。
enum { MAX = 10 };
explicit GenericArray(int ss = MAX);
template
GenericArray::GenericArray(int ss) : capacity(ss),counts(0)
{
itemsFlag = 0;
preitems = new T[capacity];
items = nullptr;
}
2.析构函数
负责释放已分配的内存资源。
template
GenericArray::~GenericArray()
{
if (preitems != nullptr)
delete[]preitems;
if (items != nullptr)
delete[]items;
}
3.检查下标
验证待操作的下标是否处于当前数组容量的合法范围内。
template
bool GenericArray::checkIndex(int index)
{
if (index < 0 || index >= capacity)
{
int cap = capacity - 1;
cout << "Out of the range! Please ensure the index be
in 0 ~ " << cap << '\n';
return false;
}
return true;
}
4.获取元素数目和容量、判断数组空和满
int count()const { return counts; }
int getCapacity()const { return capacity; }
bool isEmpty()const { return counts == 0; }
bool isFull()const { return counts >= capacity; }
5.取索引对应值、按索引修改值、打印输出、是否包含某值
template
T GenericArray::get(int index)
{
if (!itemsFlag)
return preitems[index];
else
return items[index];
}
void GenericArray::set(int index, T elem)
{
if(checkIndex(index))
{
if (!itemsFlag)
preitems[index] = elem;
else
items[index] = elem;
return;
}
}
template
void GenericArray::printArray()const
{
for (int i = 0; i < counts; i++)
if (!itemsFlag)
cout << preitems[i] << '\t';
else
cout << items[i] << '\t';
cout << '\n';
return;
}
template
bool GenericArray::contains(T arr)
{
for (int i = counts - 1; i >= 0; i--)
if (!itemsFlag)
{
if (arr == preitems[i])
return true;
}
else
{
if (arr == items[i])
return true;
}
return false;
}
6.查找某值下标、删除某值
在查找特定值的下标时,需处理该值在数组中重复出现的情况。因此,定义了一个名为 findArrIndex 的结构体,用于记录目标值出现的总次数及其对应的所有下标位置。
struct findArrIndex
{
int numIndex;
int *findIndex;
};
template
void GenericArray::find(T arr, findArrIndex *ps)
{
ps->findIndex = new int[counts];
ps->numIndex = 0;
for (int i = 0, j = 0; i < counts; i++, j++)
if (!itemsFlag)
{
if (arr == preitems[i])
{
(ps->findIndex)[j] = i;
(*ps).numIndex++;
cout << i << '\t';
}
}
else
if (arr == items[i])
{
(ps->findIndex)[j] = i;
(*ps).numIndex++;
cout << i << '\t';
}
cout << '\n';
return;
}
template
void GenericArray::removeElement(findArrIndex *ps)
{
for (int i = ps->numIndex; i > 0; i--)
remove((ps->findIndex)[i - 1]);
delete[](ps->findIndex);
}
template
void GenericArray::set(int index, T elem)
{
if(checkIndex(index))
{
if (!itemsFlag)
preitems[index] = elem;
else
items[index] = elem;
return;
}
}
7.扩容
在执行添加数据操作前,需评估当前数组容量是否充足。若容量不足,则必须触发扩容机制以容纳新数据。
template
void GenericArray::renewCapacity()
{
cout << "The array's capacity is small! Renew capacity.\n";
if (capacity < 1000)
capacity = capacity << 1;
else
capacity = capacity >> 1 + capacity;
if (!itemsFlag)
{
itemsFlag = 1;
items = new T[capacity];
for (int i = 0; i
8.添加数据:数组添加数据包括按索引下标插值、数组头插值、数组尾插值。实质上后两种都可以通过调用按索引下标插值函数实现。前文也提到过,数组添加操作中复杂的是大量的数据搬移工作:将某个元素按索引下标插入到数组第k个位置,需要将k ~ n部分的元素向后搬移一位,然后插入元素,更新元素数目。若插入到数组尾,时间复杂度O(1);插入到数组头,时间复杂度O(n);插入的平均时间复杂度为(1+2+…+n)/n = O(n)。 另外,还有一个优化问题:若数组是无序数组,则插入时不需要搬移数据:若将某个元素插入到数组第k个位置,首先将该位置的元素移动到数组末尾,然后将待插入元素插入到第k个位置,时间复杂度O(1)。
template
void GenericArray::add(int index, T elem)
{
if (isFull())
{
cout << "Array is full!" << '\n';
renewCapacity();
}
if (checkIndex(index))
if(!itemsFlag)
{
for (int i = counts; i > index; i--)
preitems[i] = preitems[i - 1];
preitems[index] = elem;
}
else
{
for (int i = counts; i > index; i--)
items[i] = items[i - 1];
items[index] = elem;
}
counts++;
return;
}
template
void GenericArray::addFirst(T elem)
{
add(0, elem);
}
template
void GenericArray::addLast(T elem)
{
add(counts, elem);
}
9.删除数据:数组删除数据包括按索引下标删除、数组头删除、数组尾删除。实质上后两种都可以通过调用按索引下标删除函数实现。与前文类似,数组删除操作中复杂的也是大量的数据搬移工作:按索引下标将某个元素删除,需要将k+1 ~ n部分的元素向前搬移一位,更新元素数目。若删除数组尾,直接元素数目减一即可,时间复杂度O(1);删除数组头,时间复杂度O(n);删除的平均时间复杂度(1+2+…+n)/n = O(n)。 另外,有一个优化问题:如果想多次删除数组中的值,可以先对要删除的值做好标记,做完标记后一次删除,这样就大大减少了搬移的次数。
template
T GenericArray::remove(int index)
{
if (!isEmpty())
{
if (checkIndex(index))
{
if (!itemsFlag)
{
T temp = preitems[index];
for (int i = index+1; i < counts; i++)
preitems[i - 1] = preitems[i];
counts--;
return temp;
}
else
{
T temp = items[index];
for (int i = index + 1; i < counts; i++)
items[i - 1] = items[i];
counts--;
return temp;
}
}
}
else
{
cout << "Array is empty!" << '\n';
return -1;
}
}
template
T GenericArray::removeFirst()
{
return remove(0);
}
template
T GenericArray::removeLast()
{
return remove(counts - 1);
}
好啦,基本上就这么多了。
最后总结一下,多看源码还是很重要的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。