震惊!!小白第一篇文章,极其敷衍!!

及其敷衍的一篇文章,有一处小错误,评论区大神可以留言指出改正!!!

检验大家C语言学习成果的时候到了!!!!!!!!!!!

///顺序表操作///

#include <stdio.h>
#include <stdlib.h>

//int last = 5;//全局
//int a[100] = {11,22,33,44,55};//数组,用来存储有效数据

#define N 100
typedef struct 
{
    int last;//last时刻代表有效元素的个数
    int data[N];//用来存储有效数据
}seqlist_t;
//sequence 顺序
//list 表

//1.遍历顺序表,将所有的有效元素打印输出
void showSeqlist(seqlist_t* p)//实参初始化形参 seqlist_t* p = p(main函数中的p)
{
    int i;
    for(i = 0; i < p->last; i++)
    {
        printf("%d ",p->data[i]);
    }
    printf("\n");
}

//2.创建一个空的顺序表
seqlist_t* createEmptySeqlist()
{
     seqlist_t* p = (seqlist_t*)malloc(sizeof(seqlist_t));//堆区
    if(p == NULL)
    {
        printf("malloc failed!!\n");
        return NULL;//提前结束函数
    }
    p->last = 0;//因为创建的是空的顺序表,所以last的值初始化为0,有效元素个数为0
    return p;
}
//3. 向指定的位置插入数据
int insertIntoSeqlist(seqlist_t* p, int post, int x) //seqlist_t* q = p;
{
    //0.容错判断,对插入位置是否合理,进行条件判断
    if(post < 1 || post > p->last+1 || isFullSeqlist(p))
    {
        printf("insertIntoSeqlist failed!!\n");
        return -1;//通常用-1来表达失败
    }
    
    //1.将最有一个有效元素的下标到插入位置的下标整体向后一个位置
    int i;
    for(i = p->last-1; i >= post-1; i--)
        p->data[i+1] = p->data[i];
    //2.插入数据
    p->data[post-1] = x;
    //3.有效元素个数+1
    p->last++;
    return 0;//用0来表达成功
}
//4.判断顺序表是否为满 满返回值是1,未满是0 
int isFullSeqlist(seqlist_t* p)
{
    /*
    //方法一:有效元素个数 == 数组的长度,表满
    if(p->last == N)
        return 1;
    else
        return 0;
    //方法二:
    return p->last == N ? 1 : 0;
    */
    //方法三 
    return p->last == N;//p->last == N 表达式为真,C语言中,真用1来表达, p->last == N表达式为假,C语言中假用0来表达
}
//5. 判断是否为空 空返回值是1,未空返回值0
int isEmptySeqlist(seqlist_t* p)
{
    return p->last == 0 ? 1 : 0;
}
//6.删除指定位置的数据
int deleteFromSeqlist(seqlist_t* p, int post)
{
    int i;
    //0.容错判断 对删除位置是否合理,进行判断
    if(post < 1 || post > p->last || isEmptySeqlist(p))
    {
        printf("deleteFromSeqlist failed!!\n");
        return -1;
    }
    //1.将删除位置的后一个位置的下标到最后一个有效元素下标,整体向前移动一个位置,覆盖删除
    for(i = post; i <= p->last-1; i++)
    {
        p->data[i-1] = p->data[i];
    }
    //2.有效元素个数-1
    p->last--;
    return 0;
}
//7.求顺序表的长度,长度就是有效元素的个数
int getLengthSeqlist(seqlist_t* p)
{
    return p->last;
}
//8.清空顺序表
void clearSeqlist(seqlist_t* p)
{
    p->last = 0;
}
//9.查找指定数据出现的位置,返回数组中的位置下标
//int x,代表被查找的数据
int searchDataSeqlist(seqlist_t* p, int x)
{
    int i;
    for(i = 0; i < p->last; i++)
    {
        if(p->data[i] == x)
            return i;
    }
    return -1;//代表没有找到
}

int main(int argc, const char *argv[])
{
    seqlist_t* p = createEmptySeqlist();//得到malloc申请结构体变量空间大小的首地址
    insertIntoSeqlist(p, 1, 11);//将首地址告诉insertIntoSeqlist函数
    insertIntoSeqlist(p, 2, 22);
    insertIntoSeqlist(p, 3, 33);
    insertIntoSeqlist(p, 4, 44);
    insertIntoSeqlist(p, 5, 55);
    insertIntoSeqlist(p, 3, 1000);
    showSeqlist(p);//将首地址告诉showSeqlist函数
    deleteFromSeqlist(p,3);
    showSeqlist(p);
    printf("len is %d\n",getLengthSeqlist(p));
    printf("33的位置下标是%d\n",searchDataSeqlist(p,33));
    clearSeqlist(p);
    printf("len is %d\n",getLengthSeqlist(p));
    free(p);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值