顺序存储的插入与删除

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值