1 Star 0 Fork 0

CoderQi/cg初学

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Seqlist1228.c 980 Bytes
一键复制 编辑 原始数据 按行查看 历史
CoderQi 提交于 2022-12-28 21:02 +08:00 . 顺序表实现
#include"Seqlist1228.h"
void slprint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void seqlistinit(SL* ps)
{
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void seqlistpushback(SL* ps, sldatatype x)
{
//如果无空间或者空间不足,那么就扩容
if (ps->size == ps->capacity)//是否扩容的条件
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
sldatatype* tmp = realloc(ps->a, newcapacity * sizeof(sldatatype));
if (tmp == NULL)//realloc失败
{
printf("realloc fail\n");
exit(-1);
}
//realloc成功了
ps->a = tmp;//把申请到的空间给 a
ps->capacity = newcapacity;// 容量给扩容后的容量
}
//插入数据
ps->a[ps->size] = x;
ps->size++;
/*ps->a[ps -> size] = x;
ps->size++;直接插入*/
}//尾插
void seqlistpopback(SL* ps);//尾删
void seqlistpushfront(SL* ps, sldatatype x);//头插
void seqlistpopfront(SL* ps);//头删
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/programmercg/cg-beginner.git
git@gitee.com:programmercg/cg-beginner.git
programmercg
cg-beginner
cg初学
master

搜索帮助