seqlist_t *CreateEpSeqlist() //返回申请的首地址给主函数
{
seqlist_t *p = (seqlist_t *)malloc(sizeof(seqlist_t));
if (NULL == p)
{
printf("CreatEpSeqList malloc fatled\n");
return NULL;
}
p->last = -1;
return p;
}
//向顺序表的指定位置插入数据
int InsertIntoSeqlist(seqlist_t *p, int post, int data)
{
if (post < 0 || post > p->last + 1 || IsFullSeqlist((p)))
{
printf("InsertIntoSeqlist failed\n");
return -1;
}
for (int i = p->last; i >= post; i--)
{
p->data[i + 1] = p->data[i];
}
p->data[post] = data;
p->last++; //让last代表最后一个有效元素的下标
return 0;
}
//遍历输出顺序表里的数据
void ShowSeqlist(seqlist_t *p)
{
for (int i = 0; i <= p->last; i++)
{
printf("%d ", p->data[i]);
}
printf("\n");
}
//判断顺序表是否为满,满为1,不满为0
int IsFullSeqlist(seqlist_t *p)
{
return p->last + 1 == N;
}
//判断顺序表是否为空
int IsEpSeqlist(seqlist_t *p)
{
return p->last == -1;
}
//删除顺序表中指定位置的数据
int DeletePostSeqlist(seqlist_t *p, int post)
{
if (post < 0 || post > p->last||IsEpSeqlist(p))
{
printf("DeletePostSeqlist failed\n");
return -1;
}
for(int i=post+1;i<=p->last;i++)
{
p->data[i-1]=p->data[i];
}
p->last--;
return 0;
}
顺序存储的插入与删除
最新推荐文章于 2024-07-16 16:49:22 发布