顺序表
概念:顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
分类:1.静态顺序表:使用定长数组存储元素
2.动态顺序表:使用动态开辟的内存存储
我们这里主要讲解动态的顺序表增删查改等操作
- 我们把动态顺序表存放在一个结构体当中
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a; //a指向顺序表的起始位置
int size; //size表示顺序表中的有效数据的个数
int capacity; //capacity表示顺序表的容量
}SeqList;
判断顺序表是否需要扩容
void CheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newcapacity;
}
}
顺序表的初始化
void SeqListInit(SeqList* ps)
{
assert(ps); //断言一下ps是否为空指针
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
顺序表的销毁
void SeqListDestroy(SeqList* ps)
{
assert(ps);
if (ps->a != NULL)
{
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
}
}
顺序表的尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
if (ps->size == ps->capacity) //判断是否需要扩容
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2; // 如果capacity为0,则开辟4个SLDateType类型数据的空间,如果capacity不为0,则将顺序表的容量扩大为2倍。
SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->capacity = newcapacity;
ps->a = tmp;
}
ps->a[ps->size] = x;
ps->size++; //增加数据的最后,不要忘记将size++
}
顺序表的头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
//还是先判断是否需要扩容
if (ps->size == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * newcapacity);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newcapacity;
}
//将数据依次向后覆盖
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++; //size不要忘记++
}
顺序表的尾删
void SeqListPopBack(SeqList* ps)
{
assert(ps);
//温柔的检查
//if (ps->size == 0)
//{
// return;
//}
//暴力检查
assert(ps->size > 0);
//直接将size--即可,但是在删除数据时需要注意size要大于0
ps->size--;
}
顺序表的头删
void SeqListPopFront(SeqList* ps)
{
assert(ps);
//还是要先断言一下size是否大于0
assert(ps->size > 0);
//将数据依次向前覆盖
for (int i = 1; i < ps->size; i++)
{
ps->a[i - 1] = ps->a[i];
}
ps->size--;
}
在顺序表下标为pos的位置插入数据
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
//插入的位置要在顺序表内
assert(pos >= 0 && pos <= ps->size);
//判断是否需要扩容
CheckCapacity(&ps);
//向后挪动数据
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
删除顺序表下标为pos的数据
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
//将数据从前向后覆盖
int begin = pos + 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}