一、概念
顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
简单来说,顺序表是用一段连续的空间储存数据的线性结构,一般使用数组存储。
顺序表分为静态顺序表和动态顺序表
静态顺序表开辟一片固定的空间,空间不够时无法扩容,若开辟空间过大则容易造成浪费。
因此我们一般使用的是动态顺序表,预先开辟一小片空间,不够时再扩容。
二、代码实现
这里我们创建三个模块进行代码实现
SeqList.h -- 函数的声明
SeqList.c -- 函数的定义
3.c -- 测试函数的功能
1、顺序表的定义
在头文件中使用结构体构建一个顺序表
2、顺序表的初始化
由于静态顺序表存在开辟空间时不够或浪费的问题
这里我们使用malloc函数动态开辟一片空间
3、顺序表的插入删除
顺序表的增删查改包括头插、尾插、头删、尾删以及在指定位置pos处进行插入删除等
对于顺序表的头插、头删、尾插、尾删操作,我们可以看做在顺序表的头位置或尾位置进行插入删除操作,即pos为头位置或尾位置
同时在插入时需要检查开辟的空间是否够用,如果不够用则需要进行扩容
4、顺序表的查找
查找操作较为简单,对顺序表进行遍历即可
5、顺序表的打印
6、顺序表的销毁
顺序表初始化的时候是用malloc函数向系统申请的空间,malloc函数申请的空间是在内存的堆区,堆区的空间不会被系统自动回收,需要用free函数释放空间。
三、源码
1、 SeqList.h(函数的声明)
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
int size;
int capacity;
}SeqList;
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);
void SeqListPrint(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, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);
2、SeqList.c(函数的定义)
#include"SeqList.h"
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
if (ps->a == NULL)
{
perror("malloc fail");
return;
}
ps->capacity = 4;
ps->size = 0;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListCheckCapacity(SeqList* ps)
{
assert(ps);
SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * 2);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity *= 2;
}
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
SeqListCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
SeqListInsert(ps, ps->size , x);
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
SeqListInsert(ps, 0, x);
}
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
int start = pos + 1;
while (start < ps->size)
{
ps->a[start - 1] = ps->a[start];
start++;
}
ps->size--;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
SeqListErase(ps, ps->size-1);
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
SeqListErase(ps, 0);
}
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
3、 3.c(测试函数的功能)
这里测试函数的部分功能,仅供参考
#include"SeqList.h"
menu()
{
printf("***********************\n");
printf("1、头插数据 2、尾插数据\n");
printf("3、头删数据 4、尾删数据\n");
printf("5、插入数据 6、删除数据\n");
printf("7、查找数据 8、打印数据\n");
printf(" -1、退出程序 \n");
printf("***********************\n");
}
int main()
{
int input = 0;
SeqList ps;
SeqListInit(&ps);
while (input != -1)
{
menu();
printf("请输入你的选择:>");
scanf("%d", &input);
if (1 == input)
{
int n = 0;
printf("请输入要头插的数据个数,并依次输入数据:>");
scanf("%d", &n);
int x = 0;
while (n > 0)
{
scanf("%d", &x);
SeqListPushFront(&ps, x);
n--;
}
}
else if (2 == input)
{
int n = 0;
printf("请输入要尾插的数据个数,并依次输入数据:>");
scanf("%d", &n);
int x = 0;
while (n > 0)
{
scanf("%d", &x);
SeqListPushBack(&ps, x);
n--;
}
}
else if (3 == input)
{
int n = 0;
printf("请输入要头删的数据个数,并依次输入数据:>");
scanf("%d", &n);
int x = 0;
while (n > 0)
{
scanf("%d", &x);
SeqListPopFront(&ps, x);
n--;
}
}
else if (4 == input)
{
int n = 0;
printf("请输入要尾删的数据个数,并依次输入数据:>");
scanf("%d", &n);
int x = 0;
while (n > 0)
{
scanf("%d", &x);
SeqListPopBack(&ps, x);
n--;
}
}
else if (-1 == input)
{
SeqListPrint(&ps);
}
else if (0 == input)
{
break;
}
else
printf("输入错误,请重新选择");
}
SeqListDestroy(&ps);
return 0;
}