前言:
- 本篇博客整理了数据结构初阶的线性表以及顺序表的相关知识
- 知识总结详细,还有丰富的插图帮助理解
- 代码实现:C语言
文章目录
1. 线性表
1.1 线性表的相关概念
线性表(linear list)是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串……
线性表在逻辑上是线性结构,也就是说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
2. 顺序表
2.1 顺序表的概念及结构
顺序表是用一段物理地址连续的存储单元依次存储数据的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
- 静态顺序表:使用定长数组存储元素。
静态顺序表 – N太小,可能不够用,N太大,可能浪费空间- 动态顺序表:使用动态开辟的数组存储。
动态顺序表按需申请内存,空间不够则增容
2.2 顺序表接口函数
静态顺序表只适用于确定知道需要存多少数据的场景。静态顺序表的定长数组中N是固定的,空间开多了浪费,开少了不够用。所以现实中基本都是使用动态顺序表,根据需要动态地分配空间大小。
下面实现动态顺序表的增删查改
typedef int SLDataType;
// 顺序表的动态存储
typedef struct SeqList
{
SLDateType* a;//指向动态开辟的数组
int size; //有效数据个数
int capacity; //容量大小
}SL;
// 基本增删查改接口
// 顺序表初始化
void SLInit(SL* ps);
// 检查空间,如果满了,进行增容
void SLCheckCapacity(SL* ps);
// 顺序表尾插 O(1)
void SLPushBack(SL* ps, SLDateType x);
// 顺序表尾删 O(1)
void SLPopBack(SL* ps);
// 顺序表头插 O(n)
void SLPushFront(SL* ps, SLDateType x);
// 顺序表头删 O(n)
void SLPopFront(SL* ps);
// 顺序表查找
int SLFind(SL* ps, SLDateType x);
// 顺序表修改pos位置的值
void SLModify(SL* ps, int pos, SLDateType x);
// 顺序表在pos位置插入x
void SLInsert(SL* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SLErase(SL* ps, int pos);
// 顺序表销毁
void SLDestroy(SL* ps);
// 顺序表打印
void SLPrint(SL* ps);
下面逐个实现这些函数
2.2.1 顺序表初始化
// 顺序表初始化
void SLInit(SL* ps)
{
assert(ps != NULL);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
2.2.2 检查空间,如果满了,进行增容
void SLCheckCapacity(SL* ps)
{
assert(ps != NULL);
//检查容量空间,满了扩容
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SLDateType* tmp = (SLDateType*)realloc(ps->a, newCapacity * sizeof(SLDateType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);//结束整个程序
//return;//结束该函数
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
2.2.3 顺序表尾插 O(1)
- 第一种写法
void SLPushBack(SL* ps, SLDateType x)
{
assert(ps != NULL);
SLCheckCapacity(ps);
ps->a[ps->size] = x;
ps->size++;
}
- 第二种写法(推荐✔✔✔)
void SLPushBack(SL* ps, SLDateType x)
{
//复用
SLInsert(ps, ps->size, x);
}
2.2.4 顺序表尾删 O(1)
- 第一种写法
void SLPopBack(SL* ps)
{
//温柔检查
//if (ps->size == 0)
//{
// printf("SeqList is empty\n");
// return;
//}
//暴力检查
assert(ps != NULL);
assert(ps->size > 0);
ps->size--;
}
- 第二种写法(推荐✔✔✔)
void SLPopBack(SL* ps)
{
//复用
SLErase(ps, ps->size - 1);
}
2.2.5 顺序表头插 O(n)
- 第一种写法
void SLPushFront(SL* ps, SLDateType x)
{
assert(ps != NULL);
SLCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[0] = x;
ps->size++;
}
- 第二种写法(推荐✔✔✔)
void SLPushFront(SL* ps, SLDateType x)
{
//复用
SLInsert(ps, 0, x);
}
2.2.6 顺序表头删 O(n)
- 第一种写法
void SLPopFront(SL* ps)
{
assert(ps != NULL);
assert(ps->size > 0);
int begin = 1;
while (begin < ps->size)
{
ps->a[begin - 1] = ps->a[begin];
begin++;
}
ps->size--;
}
- 第二种写法(推荐✔✔✔)
void SLPopFront(SL* ps)
{
//复用
SLErase(ps, 0);
}
2.2.7 顺序表查找
int SLFind(SL* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
2.2.8 顺序表修改pos位置的值
void SLModify(SL* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
ps->a[pos] = x;
}
2.2.9 顺序表在pos位置插入x(核心代码)
void SLInsert(SL* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
//挪动数据
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
2.2.10 顺序表删除pos位置的值(核心代码)
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
int begin = pos;
while (begin < ps->size-1)
{
ps->a[begin] = ps->a[begin + 1];
++begin;
}
//int begin = pos+1;
//while (begin < ps->size)//控制边界
//{
// ps->a[begin-1] = ps->a[begin];
// ++begin;
//}
ps->size--;
}
2.2.11 顺序表销毁
void SLDestroy(SL* ps)
{
assert(ps != NULL);
if (ps->a)
{
free(ps->a);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
}
2.2.12 顺序表打印
void SLPrint(SL* ps)
{
assert(ps != NULL);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
2.3 顺序表总结
顺序表的优缺点及问题
- 顺序表的尾插尾删时间复杂度是O(1),头插头删时间复杂度是O(n),所以顺序表适合尾插尾删。
- 写一个顺序表可以先写Insert和Erase两个接口函数,头插头删、尾插尾删可以直接复用。
- 中间/头部插入删除,时间复杂度为O(n)。
- 增容需要申请新的空间,拷贝数据,释放旧的空间。会有不小的消耗。
- 增容一般是呈2倍增长,势必会有一定的空间浪费。
想要解决上面的问题,我们需要学习链表
学习记录:
- 本篇博客整理于2022.6.21
- 如果有错误的地方请多多指教👀