前言
线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
先介绍最为简单的顺序表。
一、概念及结构
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
顺序表一般可以分为:
静态顺序表:使用定长数组存储元素。
结构设计
#define MAX_NUM 100
typedef int SLDataType;
//静态的
struct SeqList
{
SLDataType arr[MAX_NUM];//定长的数组,之后不在改变
int size;//数组中存放了多少个数据
};
动态顺序表:使用动态开辟的数组存储。
结构设计
//动态的
typedef int SLDataType;
struct SeqList
{
SLDataType* a;//一个数据类型的指针,指向动态开辟的空间
int size; //数组中存放了多少个数据
int capacity; //数组实际能存数据的个数
};
typedef struct SeqList SL;
我们一般使用动态的,能够支持扩容。以下均以动态的设计函数。
二、接口实现
接下来分别实现顺序表的增删查改等基本接口。
初始化
// 顺序表初始化
void SeqListInit(SL* psl, int capacity)
{
psl->a = (SLDataType*)malloc(sizeof(SLDataType)*capacity);
if (psl->a == NULL)
exit(-1);
psl->capacity = capacity;
psl->size = 0;
}
销毁
// 顺序表销毁
void SeqListDestory(SL* psl)
{
assert(psl);//断言
free(psl->a);//释放
psl->a = NULL;
psl->capacity = psl->size = 0;
}
打印
// 顺序表打印
void SeqListPrint(SL* psl)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
尾删
// 顺序表尾删
void SeqListPopBack(SL* psl)
{
assert(psl);
psl->size--;
}
尾插
// 顺序表尾插
void SeqListPushBack(SL* psl, SLDataType x)
{
//判断顺序表是否满了,满了就扩容
SeqListCheckCapacity(psl);
psl->a[psl->size] = x;
++psl->size;
}
// 检查空间,如果满了,进行增容
void SeqListCheckCapacity(SL* psl)
{
assert(psl);
if (psl->size == psl->capacity)
{
//扩容
int newcapacity = psl->capacity * 2;//以两倍扩充
SLDataType* new = (SLDataType*)realloc(psl, sizeof(SLDataType) * newcapacity);//扩大两倍,如果psl->a==NULL,realloc相当于malloc
if (new== NULL)
{
printf("realloc fail\n");
exit(-1);
}
psl->a = new;
psl->capacity = newcapacity;
}
else
return;
}
头删
// 顺序表头删
void SeqListPopFront(SL* psl)
{
assert(psl);
//删除第一个元素,后面的往前移动
int begin = 0;
while (begin < psl->size-1)
{
//begin最大为psl->size-2
psl->a[begin] = psl->a[begin + 1];
++begin;
}
psl->size--;
}
头插
// 顺序表头插
void SeqListPushFront(SL* psl, SLDataType x)
{
//判断是否满了
SeqListCheckCapacity(psl);
//从后往前移动
int end = psl->size-1;//end为最后一个的下标
while (end >= 0)
{
//end最小为0
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[0] = x;
psl->size++;
}
查找
// 顺序表查找,返回下标
int SeqListFind(SL* psl, SLDataType x)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
return i;
}
//失败返回-1
return -1;
}
指定位置插入
// 顺序表在pos位置插入x
void SeqListInsert(SL* psl, int pos, SLDataType x)
{
//判断是否满
SeqListCheckCapacity(psl);
//判断pos是否越界
if (pos < 0 || pos >= psl->size)
{
printf("pos error\n");
return;
}
else
{
//往后移动,类似头插
int end = psl->size - 1;
while (end >= pos)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[pos] = x;
psl->size++;
}
}
指定下标删除
// 顺序表删除pos位置的值
void SeqListErase(SL* psl, int pos)
{
//往前移,类似头删
assert(psl);
int begin = pos;
while (begin < psl->size - 1)
{
psl->a[begin] = psl->a[begin + 1];
++begin;
}
psl->size--;
}
测试
建议写一个函数测一次(分别写不同的测试函数)
#include"SeqList.h"
void SLtest1()
{
SL sl;
SeqListInit(&sl, 4);
SeqListPushBack(&sl, 1);
SeqListPushBack(&sl, 2);
SeqListPushBack(&sl, 3);
SeqListPushBack(&sl, 4);
SeqListPushBack(&sl, 5);
SeqListPrint(&sl);
SeqListPushFront(&sl, 1);
SeqListPushFront(&sl, 2);
SeqListPushFront(&sl, 3);
SeqListPushFront(&sl, 4);
SeqListPushFront(&sl, 5);
SeqListPrint(&sl);
SeqListPopBack(&sl);
SeqListPopBack(&sl);
SeqListPopFront(&sl);
SeqListPopFront(&sl);
SeqListPrint(&sl);
SeqListErase(&sl, SeqListFind(&sl, 1));
SeqListPrint(&sl);
SeqListInsert(&sl, 2, 10);
SeqListPrint(&sl);
SeqListDestory(&sl);
}
int main()
{
SLtest1();
return 0;
}
三、顺序表优缺点
优点
- 逻辑顺序与物理顺序一致。、
- 支持随机访问,即不需要查找,可访问任意项数据,读取速率快。
- 无需为表示结点之间的逻辑关系而增加额外空间,存储利用率高。
缺点
- 中间/头部的插入删除,时间复杂度为O(N)
- 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
- 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。