栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

///顺序表操作///

#include
#include

//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)
{
    
    //方法三 
    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 %dn",getLengthSeqlist(p));
    printf("33的位置下标是%dn",searchDataSeqlist(p,33));
    clearSeqlist(p);
    printf("len is %dn",getLengthSeqlist(p));
    free(p);
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1021976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号