顺序表
🎈1.线性表
线性表是
N
个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
✅线性表在逻辑上是线性结构,也就是说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
🎈2.顺序表
🔭2.1概念及结构
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
✅顺序表一般可以分为:
- 静态顺序表:使用定长数组存储元素。
- 动态顺序表:使用动态开辟的数组存储。
//动态存储
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;//指定动态开辟的数组
int size;//有效数据
int capacity;//有效容量
}SL;
🔭2.2接口实现
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺初表的定长数组导致N定大了,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态的分配空间大小,所以下面我们实现动态顺序表。
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;//指定动态开辟的数组
int size;//有效数据
int capacity;//有效容量
}SL;
void SLInit(SL* psl);//初始化
void SLDestroy(SL* psl);//销毁
void SLPrint(SL* psl);//打印
void SLCheckCapacity(SL* psl);//判断是否需要扩容
void SLPushBack(SL* psl, SLDataType x);//尾插
void SLPushFront(SL* psl, SLDataType x);//头插
void SLPopBack(SL* psl);//尾删
void SLPopFront(SL* psl);//头删
void SLInsert(SL* psl, int pos, SLDataType x);// 任意下标位置的插入
void SLErase(SL* psl, int pos);// 任意下标位置的删除
int SLFind(SL *psl,int pos,SLDateType x);//寻找元素下标
🎈3.动态顺序表的功能实现
🔭3.1初始化
void SLInit(SL* psl)
{
assert(psl);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
🔭3.2销毁
void SLDestroy(SL* psl)
{
assert(psl);
if (psl->a != NULL)
{
free(psl->a);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
}
🔭3.3打印
void SLPrint(SL* psl)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
cout << psl->a[i] << " ";
}
cout << endl;
}
🔭3.4判断是否需要扩容
void SLCheckCapacity(SL* psl)
{
assert(psl);
if (psl->size == psl->capacity)
{
int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
SLDataType* temp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * newCapacity);
if (temp == NULL)
{
perror("realloc fail");
return;
}
psl->a = temp;
psl->capacity = newCapacity;
}
}
🔭3.5尾插
void SLPushBack(SL* psl, SLDataType x)
{
assert(psl);
SLCheckCapacity(psl);
psl->a[psl->size] = x;
psl->size++;
}
🔭3.6头插
void SLPushFront(SL* psl, SLDataType x)
{
assert(psl);
SLCheckCapacity(psl);
SLDataType end = psl->size - 1;
while (end >= 0)
{
psl->a[end + 1] = psl->a[end];
--end;
}
psl->a[0] = x;
psl->size++;
}
🔭3.7尾删
void SLPopBack(SL* psl)
{
assert(psl);
assert(psl->size > 0);
psl->size--;
}
🔭3.8头删
void SLPopFront(SL* psl)
{
assert(psl);
assert(psl->size > 0);
int begin = 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
++begin;
}
psl->size--;
}
🔭3.9任意下标位置的插入
void SLInsert(SL* psl, int pos, SLDataType x)
{
assert(psl);
assert(pos >= 0 && pos <= psl->size);
SLCheckCapacity(psl);
//挪动数据
int end = psl->size - 1;
while (end >= pos)
{
psl->a[end + 1] = psl->a[end];
--end;
}
psl->a[pos] = x;
psl->size++;
}
🔭3.10任意下标位置的删除
void SLErase(SL* psl, int pos)
{
assert(psl);
assert(pos >= 0 && pos < psl->size);
//挪动覆盖
int begin = pos + 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
++begin;
}
psl->size--;
}
🔭3.11寻找元素下标
int SLFind(SL* psl, int pos, SLDataType x)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
{
return i;
}
}
return -1;
}
好啦,关于数据结构的顺序表到这里就先结束啦,后期会继续更新学习数据结构初阶的相关知识,欢迎大家持续关注、点赞和评论!❤️❤️❤️