文章目录
数据结构:线性表之顺序表
1. 顺序表的概念
顺序表使用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储,在数组上完成数据的增删查改。
2. 顺序表的结构
顺序表一般分为:
- 静态顺序表:使用定长数组存储数据。
- 动态顺序表:使用动态开辟的数组存储。
3. 接口的实现
静态顺序表只适用于确定知道需要多少存储空间的场景,而实际上需要的存储空间的大小是不确定的;此外,静态顺序表的定长数组导致长度N确定,空间开大了会引起浪费,开小了又不够用。所以,实际中基本都是使用动态顺序表,根据实际需要,动态地分配空间大小。接下来也只讨论动态顺序表。
3.1 定义
SeqList.h
文件:
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;//指向动态开辟的数组
size_t size;//当前有序数据个数
size_t capacity; // 容量空间的大小
}SeqList;
// 基本增删改查接口
//顺序表初始化
void SeqListInit(SeqList* ps);
//顺序表的销毁
void SeqListDestroy(SeqList* ps);
//顺序表的打印
void SeqListPrint(SeqList* ps);
//检查顺序表容量,如果满了,就扩容
void CheckCapacity(SeqList* ps);
//顺序表的尾插
void SeqListPushBack(SeqList* ps, SLDateType x);
//顺序表的头插
void SeqListPushFront(SeqList* ps, SLDateType x);
//顺序表的头删
void SeqListPopFront(SeqList* ps);
//顺序表的尾删
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);
3.2 实现
SeqList.c
文件:
3.2.1 初始化
//初始化
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
3.2.2 销毁
//销毁
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
3.2.3 打印
//打印
void SeqListPrint(SeqList* ps)
{
assert(ps);
assert(ps->size > 0);
int i = 0;
for (i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
3.2.4 检查容量,扩容
//检查容量
void CheckCapacity(SeqList* ps)
{
if (ps->capacity == ps->size)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = (SLDateType*)realloc(ps->a,newCapacity * sizeof(SLDateType));
if (!tmp)
{
return;
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
3.2.5 尾插
//尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
//检查容量
CheckCapacity(ps);
//插入
//code-1
//ps->a[ps->size] = x;
//ps->size++;
//code-2
SeqListInsert(ps, ps->size, x);//此接口在下面
}
3.2.6 头插
//头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
assert(ps->size > 0);
//检查容量
CheckCapacity(ps);
//插入
//int end = ps->size-1;
//while (end>=0)
//{
// ps->a[end + 1] = ps->a[end];
// end--;
//}
//ps->a[0] = x;
//ps->size++;
//code-2
SeqListInsert(ps, 0, x);
}
3.2.7 尾删
//尾删
void SeqListPopBack(SeqList* ps)
{
//code-1
/*assert(ps);
assert(ps->size > 0);
ps->size--;*/
//code-2
SeqListErase(ps, ps->size - 1);
}
3.2.8 头删
//头删
void SeqListPopFront(SeqList* ps)
{
//code-1
/*assert(ps);
assert(ps->size > 0);
int i = 0;
for (i = 0; i < ps->size-1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;*/
//code-2
SeqListErase(ps, 0);
}
3.2.9 在pos位置插入
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
assert(ps);
assert(ps->size >= pos);//保证pos位置前都存放有数据
//扩容
CheckCapacity(ps);
//插入
//code-1
/*int end = ps->size - 1;
while (end >= (int)pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;*/
//code-2
size_t end = ps->size;
while (end > pos)
{
ps->a[end] = ps->a[end - 1];
--end;
}
ps->a[end] = x;
++ps->size;
}
如果采用code-1,那么要注意:
条件语句end >= (int)pos
这里的pos一定要强转为int
类型。
因为size_t
是无符号类型,end是int
有符号类型。比较时,会进行算术转换,都会变成无符号类型比较。
当end为0时,--end
,end就会变成一个非常大的无符号数字(2^32-1),从而此循环语句就会变成一个死循环。
3.2.10 删除pos位置的值
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
assert(ps);
assert(pos < ps->size);
int cur = (int)pos;
while (cur < ps->size - 1)
{
ps->a[cur] = ps->a[cur + 1];
cur++;
}
ps->size--;
}
3.2.11 查找
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
assert(ps->size > 0);
int i = 0;
for (i = 0; i < ps->size; i++)
{
//找到了
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
4. 顺序表的缺点
- 中间/头部的插入、删除,时间复杂度为O(1)。
- 增容需要开辟新空间,拷贝数据,释放旧空间,会有不小的消耗浪费。
- 增容一般都是增到原来的2倍,这样极易产生巨大的空间浪费。