作者:一个喜欢猫咪的的程序员
专栏:《数据结构》
喜欢的话:世间因为少年的挺身而出,而更加瑰丽。 ——《人民日报》
目录
顺序表:
顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
顺序表结构:
顺序表本质上是一段连续存放的数据,类似于数组
我们想想需要什么参数:
- 首先是连续存放的数据,我们会想到数组,但是我们的个数不能改变,所以我们需要一个指针a指向malloc开辟一段连续的空间。
- 然后当我们需要改变这个空间的大小,我们还需要一个变量来表示已有空间的大小capacity。
- 我们如何判断什么时候需要扩容呢?需要一个计算已有数据的个数变量size来判断等于已有空间的大小来决定是否扩容。
我们这里有多个数据我们可以用结构体比较方便!
typedef int SLDataType;//将int型起个别名为SLDataType,这样方便更改类型 typedef struct SeqList { SLDataType* a;//动态开辟的数组 int size;//记录储存了多少个数据 int capacity;//空间容量大小 //多个数据就可以利用结构体 }SL;
我们在本篇实现的是动态顺序表。
功能为增删查改!
我们在SeqList.h文件中进行函数声明。
SeqList.c函数进行各个函数的实现。
Test.c函数进行函数的测试,以及最后的菜单编写。
SeqLisht.c功能函数的实现:
#include"SeqList.h" void SeqListPrint(SL* s) {//打印 assert(s); for (int i = 0; i < s->size; i++) { printf("%d ", s->a[i]); } printf("\n"); } void SeqListInit(SL* s) {//初始化的实现 assert(s); s->a = NULL; s->size = 0; s->capacity = 0; } void SeqListCherkCapacity(SL* s) { assert(s);//扩容capacity if (s->capacity == s->size) { int Newcaoacity = s->capacity == 0 ? 4 : s->capacity * 2; SLDataType* tmp = (SLDataType*)realloc(s->a, Newcaoacity * sizeof(SLDataType)); //realloc可能是返回一个新的地址,为异地扩容 if (tmp == NULL) { perror("realloc faile"); exit(-1);//可以提前结束程序,为异常结束 } s->a = tmp; s->capacity = Newcaoacity; } } void SeqListDestroy(SL* s) {//销毁SeqList assert(s); if (s->a) { free(s->a); s->a = NULL; s->capacity = 0; s->size = 0; } } void SeqListPushBack(SL* s, SLDataType x) {//SeqList尾插 //扩容 assert(s);// /*SeqListCherkCapacity(s); s->a[s->size] = x; s->size++;*/ SeqListInsert(s, s->size, x); } void SeqListPopBack(SL* s) { //SeqListw尾删 //if (s->size == 0) //{//温柔的检查 // return; //} //assert(s->size>0);//暴力的检查 //s->size--; SeqListErase(s, s->size - 1); } void SeqListPushFront(SL* s, SLDataType x) {//头插 assert(s); //SeqListCherkCapacity(s); //int end = s->size - 1; //while (end >= 0) //{ // s->a[end + 1] = s->a[end]; // end--; //} //s->a[0] = x; //s->size++; SeqListInsert(s, 0, x); } void SeqListPopFront(SL* s) { //头删 assert(s); assert(s->size > 0); int begin = 0; while (begin < s->size - 1) { s->a[begin] = s->a[begin + 1]; begin++; } s->size--; SeqListErase(s, 0); } void SeqListInsert(SL* s, int pos, SLDataType x) {//任意位置插入 assert(s); assert(pos >= 0 && pos <= s->size); SeqListCherkCapacity(s); int end = s->size - 1; while (end >= pos) { s->a[end + 1] = s->a[end]; end--; } s->a[pos] = x; s->size++; } void SeqListErase(SL* s, int pos) {//任意位置删除 assert(s); assert(pos >= 0 && pos <= s->size); int begin = pos; while (begin <= s->size) { s->a[begin] = s->a[begin + 1]; begin++; } s->size--; } int SeqListFind(SL* s, SLDataType x,int begin) { assert(s); for (int i = begin; i < s->size; i++) { if (s->a[i] == x) return i; } return -1; }
SeqList顺序表的各个功能函数的实现:
基础功能:
初始化SeqList:
void SeqListInit(SL* s) {//初始化的实现 assert(s); s->a = NULL; s->size = 0; s->capacity = 0; }
销毁SeqList:
void SeqListDestroy(SL* s) {//销毁SeqList assert(s); if (s->a) { free(s->a); s->a = NULL; s->capacity = 0; s->size = 0; } }
打印SeqList:
void SeqListPrint(SL* s) {//打印 assert(s); for (int i = 0; i < s->size; i++) { printf("%d ", s->a[i]); } printf("\n"); }
扩容capacity:
void SeqListCherkCapacity(SL* s) { assert(s);//扩容capacity if (s->capacity == s->size) { int Newcaoacity = s->capacity == 0 ? 4 : s->capacity * 2; SLDataType* tmp = (SLDataType*)realloc(s->a, Newcaoacity * sizeof(SLDataType)); //realloc可能是返回一个新的地址,为异地扩容 if (tmp == NULL) { perror("realloc faile"); exit(-1);//可以提前结束程序,为异常结束 } s->a = tmp; s->capacity = Newcaoacity; } }

