石家庄网站建设公司排名,dedecms官网,开源商城小程序,上饶有哪些做网站的公司 作者简介#xff1a;დ旧言~#xff0c;目前大一#xff0c;现在学习Java#xff0c;c#xff0c;c#xff0c;Python等  座右铭#xff1a;松树千年终是朽#xff0c;槿花一日自为荣。  望小伙伴们点赞#x1f44d;收藏✨加关注哟#x1f495;#x1…  作者简介დ旧言~目前大一现在学习JavaccPython等  座右铭松树千年终是朽槿花一日自为荣。  望小伙伴们点赞收藏✨加关注哟        前言        梦回数组当我们开辟了一个数组数组的大小就已经确定了不能括容也不能减容为了解决这个问题在数据结构有一个这样的知识--《顺序表》。顺序表连续开辟一个空间能括容也能减容今天我们手撕顺序表。 主体 
咱们从三个方面实现顺序表动态管理头插头删尾插尾删增删查改。 在程序中为了实现顺序表需要创建头文件SeqList.h 创建源文件test.cSeqList.c。 动态管理 
初始化动态顺序表 
首先定义一个结构体在源文件中结构体里面有可变数组a数组初始长度size初始空间capacity。初始化动态顺序表SeqList.h中需要开辟空间再判断空间是否为空再初始化数组长度空间。 
//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{//定义一个可变数组SLDataType* a;//定义数组初始长度int size;//定义空间大小int capacity;
}SL;//初始化动态顺序表
void SLInit(SL* ps)
{//开辟空间ps-a  (SLDataType*)malloc(sizeof(SLDataType*) * 4);//判断空间是否为空if (ps-a  NULL){perror(malloc failed);exit(-1);}//初始化ps-size  0;ps-capacity  4;
} 1.防止一直创建结构体为了一劳永逸我们把动态顺序表放在源文件中。 2.这里我们用typedef int SLDataType给数据类型重定义名字这样就可以防止修改数据类型。 扩容顺序表空间 
当数组初始长度size初始空间capacity相同时这里们就需要扩容这里我们扩容两倍。在头文件中SeqList.h中 
//扩容空间
void SLCheckCapacity(SL* ps)
{//如果size和capacity相同if (ps-size  ps-capacity){//扩大两倍SLDataType* tmp  (SLDataType*)realloc(ps-a, ps-capacity * 2 * sizeof(SLDataType));//判断tmp是否为空if (tmp  NULL){perror(realloc failed);exit(-1);}//赋值ps-a  tmp;ps-capacity  ps-capacity * 2;}
} 
释放顺序表内存 
可变数组a置为NULL数组长度size置为0初始空间capacity置为0。 
//释放内存
void SLDestroy(SL* ps)
{//断言assert(ps);free(ps-a);ps-a  NULL;ps-size  0;ps-capacity  0;
} 头插头删尾插尾删 
添加元素 
首先需要断言防止顺序表为空。这里需要注意空间是否满了。这里的实现和在pos位置插入x有类似等我们实现这个函数就可以直接调用这个函数更加简便。在头文件中SeqList.h中 
//添加元素
void SLPushBack(SL* ps, SLDataType x)
{//断言assert(ps);//判断空间是否满了需要调用函数SLCheckCapacity(ps);//添加元素ps-a[ps-size]  x;ps-size;//在pos位置插入x//SLInsert(ps, ps-size, x);
} 
打印元素 
打印元素太简单啦直接用for循环就行。在头文件中SeqList.h中 
//打印数组
void SLPrint(SL* ps) 
{for (int i  0; i  ps-size; i){printf(%d , ps-a[i]);}printf(\n);
} 
尾删 
直接在末尾删除就行之后这里只要size减减就好了后面的元素会覆盖。这里的实现和在pos位置插入x有类似等我们实现这个函数就可以直接调用这个函数更加简便。(在头文件中SeqList.h中 
//尾删
void SLPopBack(SL* ps)
{//断言assert(ps);// 温柔的检查//if (ps-size  0)//return;// 暴力的检查assert(ps-size  0);//ps-a[ps-size - 1]  0;//这里只要size减减就好了后面的元素会覆盖ps-size--;//删除pos位置的值//SLErase(ps, ps-size - 1);
} 
头插重点 这里需要注意要让ps-size加加这里的实现和在pos位置插入x有类似等我们实现这个函数就可以直接调用这个函数更加简便。(在头文件中SeqList.h中 
//头插
void SLPushFront(SL* ps, SLDataType x)
{//断言assert(ps);//定义最后一个元素int end  ps-size - 1;//循环while (end  0){ps-a[end  1]  ps-a[end];end--;}//x赋给最前的数字ps-a[0]  x;//指向下一个数字ps-size;//在pos位置插入x//SLInsert(ps, 0, x);
} 
头删重点 这里需要注意要让ps-size减减这里的实现和删除pos位置的值相似等我们实现这个函数就可以直接调用这个函数更加简便。(在头文件中SeqList.h中 
//头删
void SLPopFront(SL* ps)
{//断言assert(ps);//断言(指向不能为零)assert(ps);//指向头前面的那个数字int begin  1;//循环while (begin  ps-size){ps-a[begin - 1]  ps-a[begin];begin;}ps-size--;//删除pos位置的值//SLErase(ps, 0);
} 增删查改 
在pos位置插入x 
首先这里需要判断pos是否在数组空间内其次判断空间是否充足然后循环最后ps-size 
这里的插入本质和头插相似。元素向后移动一格 //在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x)
{//断言这里posassert(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;
}删除pos位置的值 
首先这里需要判断pos是否在数组空间内其次判断空间是否充足然后循环最后ps-size-- 
这里的删除本质和头删相似。元素向前移动一格 //删除pos位置的值
void SLErase(SL* ps, int pos)
{//断言assert(ps);//断言posassert(pos  0  pos  ps-size);int begin  pos  1;while (begin  ps-size){ps-a[begin - 1]  ps-a[begin];begin;}ps-size--;
} 修改pos位置的信息 
这个实现太简单啦。 
//修改pos位置的信息
void SLModify(SL* ps, int pos, SLDataType x)
{//断言assert(ps);//判断posassert(pos  0  pos  ps-size);//直接换ps-a[pos]  x;
} 
主函数 
int main()
{//定义结构体SL sl;//初始动态列表SLInit(sl);//释放内存SLDestroy(sl);return 0;
} 
代码总结 
主函数 
//主函数包含头文件
#includeSeqList.h//尾删
void TestSeqList1()
{//定义结构体SL sl;//初始化SLInit(sl);//添加元素SLPushBack(sl, 1);SLPushBack(sl, 2);SLPushBack(sl, 3);SLPushBack(sl, 4);SLPushBack(sl, 5);SLPushBack(sl, 6);SLPushBack(sl, 6);SLPushBack(sl, 0);SLPushBack(sl, 0);//打印元素SLPrint(sl);//尾删SLPopBack(sl);SLPopBack(sl);//打印元素SLPrint(sl);//尾删SLPopBack(sl);SLPopBack(sl);SLPopBack(sl);SLPopBack(sl);SLPopBack(sl);SLPopBack(sl);SLPopBack(sl);//打印元素SLPrint(sl);//添加元素SLPushBack(sl, 1);SLPushBack(sl, 2);//打印元素SLPrint(sl);//释放内存SLDestroy(sl);
}//头删
void TestSeqList2()
{//定义结构体SL sl;//初始动态列表SLInit(sl);//添加元素SLPushBack(sl, 1);SLPushBack(sl, 2);SLPushBack(sl, 3);SLPushBack(sl, 4);SLPushBack(sl, 5);//打印元素SLPrint(sl);//头插SLPushFront(sl, 10);SLPushFront(sl, 20);SLPushFront(sl, 30);SLPushFront(sl, 40);//头删SLPopFront(sl);SLPopFront(sl);SLPopFront(sl);//打印元素SLPrint(sl);//释放内存SLDestroy(sl);
}//在pos位置插入x
void TestSeqList3()
{//定义结构体SL sl;//初始动态列表SLInit(sl);//添加元素SLPushBack(sl, 1);SLPushBack(sl, 2);SLPushBack(sl, 3);SLPushBack(sl, 4);SLPushBack(sl, 5);//打印元素SLPrint(sl);//在pos位置插入xint input  0;printf(请输入插入的位置);scanf(%d, input);int number  0;printf(请输入插入的数字);scanf(%d, number);SLInsert(sl, input, number);//打印元素SLPrint(sl);//释放内存SLDestroy(sl);
}//删除pos位置的值
void TestSeqList4()
{//定义结构体SL sl;//初始动态列表SLInit(sl);//添加元素SLPushBack(sl, 1);SLPushBack(sl, 2);SLPushBack(sl, 3);SLPushBack(sl, 4);SLPushBack(sl, 5);//打印元素SLPrint(sl);//删除pos位置的值int input  0;printf(请输入删除的位置);scanf(%d, input);SLErase(sl, input);//打印元素SLPrint(sl);//释放内存SLDestroy(sl);
}//删除pos位置的值
void TestSeqList5()
{//定义结构体SL sl;//初始动态列表SLInit(sl);//添加元素SLPushBack(sl, 1);SLPushBack(sl, 2);SLPushBack(sl, 3);SLPushBack(sl, 4);SLPushBack(sl, 5);//打印元素SLPrint(sl);//修改pos位置的信息int input  0;printf(请输入需要修改的位置);scanf(%d, input);int number  0;printf(请输入需要修改的数字);scanf(%d, number);SLModify(sl, input, number);//打印元素SLPrint(sl);//释放内存SLDestroy(sl);
}int main()
{//TestSeqList1();//TestSeqList2();//TestSeqList3();//TestSeqList4();TestSeqList5();return 0;
} 
SeqList.h源文件 
#pragma once
#includestdio.h
#includestdlib.h
#includeassert.h//动态顺序表
typedef int SLDataType;
typedef struct SeqList
{//定义一个可变数组SLDataType* a;//定义数组初始长度int size;//定义空间大小int capacity;
}SL;//动态管理
//初始化动态顺序表
void SLInit(SL* ps);
//释放内存
void SLDestroy(SL* ps);
//打印数组
void SLPrint(SL* ps);
//扩容空间
void SLCheckCapacity(SL* ps);// 头插头删 尾插尾删
void SLPushBack(SL* ps, SLDataType x);//添加元素
void SLPopBack(SL* ps);//尾删
void SLPushFront(SL* ps, SLDataType x);//头插元素
void SLPopFront(SL* ps);//头删//返回下标没有找打返回-1
int SLFind(SL* ps, SLDataType x);
//在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x);
//删除pos位置的值
void SLErase(SL* ps, int pos);
//修改pos位置的信息
void SLModify(SL* ps, int pos, SLDataType x); 
SeqList.c头文件 
#includeSeqList.h//初始化动态顺序表
void SLInit(SL* ps)
{//开辟空间ps-a  (SLDataType*)malloc(sizeof(SLDataType*) * 4);//判断空间是否为空if (ps-a  NULL){perror(malloc failed);exit(-1);}//初始化ps-size  0;ps-capacity  4;
}//释放内存
void SLDestroy(SL* ps)
{//断言assert(ps);free(ps-a);ps-a  NULL;ps-size  0;ps-capacity  0;
}//打印数组
void SLPrint(SL* ps) 
{for (int i  0; i  ps-size; i){printf(%d , ps-a[i]);}printf(\n);
}//扩容空间
void SLCheckCapacity(SL* ps)
{//如果size和capacity相同if (ps-size  ps-capacity){//扩大两倍SLDataType* tmp  (SLDataType*)realloc(ps-a, ps-capacity * 2 * sizeof(SLDataType));//判断tmp是否为空if (tmp  NULL){perror(realloc failed);exit(-1);}//赋值ps-a  tmp;ps-capacity  ps-capacity * 2;}
}//添加元素
void SLPushBack(SL* ps, SLDataType x)
{//断言assert(ps);/*//判断空间是否满了需要调用函数SLCheckCapacity(ps);//添加元素ps-a[ps-size]  x;ps-size;*///在pos位置插入xSLInsert(ps, ps-size, x);
}//尾删
void SLPopBack(SL* ps)
{//断言assert(ps);/*// 温柔的检查//if (ps-size  0)//return;// 暴力的检查assert(ps-size  0);//ps-a[ps-size - 1]  0;//这里只要size减减就好了后面的会覆盖ps-size--;*///删除pos位置的值SLErase(ps, ps-size - 1);
}//头插
void SLPushFront(SL* ps, SLDataType x)
{//断言assert(ps);/*//定义最后一个元素int end  ps-size - 1;//循环while (end  0){ps-a[end  1]  ps-a[end];end--;}//x赋给最前的数字ps-a[0]  x;//指向下一个数字ps-size;*///在pos位置插入xSLInsert(ps, 0, x);
}//头删
void SLPopFront(SL* ps)
{//断言assert(ps);/*//断言(指向不能为零)assert(ps);//指向头前面的那个数字int begin  1;//循环while (begin  ps-size){ps-a[begin - 1]  ps-a[begin];begin;}ps-size--;*///删除pos位置的值SLErase(ps, 0);
}//返回下标没有找打返回-1
int SLFind(SL* ps, SLDataType x)
{//断言assert(ps);//遍历for (int i  0; i  ps-size; i){if (ps-a[i]  x){return i;}}return -1;
}//在pos位置插入x
void SLInsert(SL* ps, int pos, SLDataType x)
{//断言这里posassert(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;
}//删除pos位置的值
void SLErase(SL* ps, int pos)
{//断言assert(ps);//断言posassert(pos  0  pos  ps-size);int begin  pos  1;while (begin  ps-size){ps-a[begin - 1]  ps-a[begin];begin;}ps-size--;
}//修改pos位置的信息
void SLModify(SL* ps, int pos, SLDataType x)
{//断言assert(ps);//判断posassert(pos  0  pos  ps-size);//直接换ps-a[pos]  x;
} 
结束语 
今天内容就到这里啦时间过得很快大家沉下心来好好学习会有一定的收获的大家多多坚持嘻嘻成功路上注定孤独因为坚持的人不多。那请大家举起自己的小说手给博主一键三连有你们的支持是我最大的动力回见。