反方向顺流的船 2022-10-02 15:20 采纳率: 60%
浏览 294
已结题

创建链表的问题,希冀平台

问题遇到的现象和发生背景

创建链表问题:提交与自己学号相邻的两位同学的学号与一门考试成绩,编程建立由这三组数据结点组成的简单链表。
我在CSDN上允许各种案例,输出都是正确的,但是到了希冀平台上就是输出有问题

用代码块功能插入代码,请勿粘贴截图

#include <stdio.h>
#include <stdlib.h>
typedef struct Lnode* node;
struct Lnode
{
int num, score;
struct Lnode next;
};
node createlist()
{
struct Lnode lis=(struct Lnode)malloc(sizeof(struct Lnode));
lis->next = NULL;
lis->num = 0;
return lis;
}
void addelement(struct Lnode
p)
{
struct Lnode newone= (struct Lnode)malloc(sizeof(struct Lnode));
newone->next = NULL;
struct Lnode* nextp=p;
for (int i = 0; i < p->num; i++)
nextp = nextp->next;
nextp->next = newone;
p->num++;

scanf("%d,%d", &newone->num, &newone->score);

}
void outelement(struct Lnode* p)
{
struct Lnode* nextp = p;

for (int t = 0; t < p->num; t++)
{
    printf("[num=%d,score=%d]", nextp->next->num, nextp->next->score);
    if (i != p->num - 1)
        printf("\n");
    nextp = nextp->next;
}

}
int main()
{
struct Lnode *p;
p = createlist();
for (int t= 0; t < 3; i++)
{
addelement(p);

}
    outelement(p);
return 0;

}

运行结果及报错内容

img

我的解答思路和尝试过的方法

有思考过空格问题,但是改了几次还是不行

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2022-10-02 17:28
    关注

    重新修改,输入时注意学号和分数间的逗号 ‘,' 是英文半角的,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct Lnode* node;
    struct Lnode
    {
        int num, score;
        struct Lnode* next;
    };
    node createlist()
    {
        struct Lnode* lis = (struct Lnode*)malloc(sizeof(struct Lnode));
        lis->next = NULL;
        lis->num = 0;
        return lis;
    }
    void addelement(struct Lnode* p)
    {
        struct Lnode* newone = (struct Lnode*)malloc(sizeof(struct Lnode));
        newone->next = NULL;
        scanf("%d,%d", &newone->num, &newone->score);
        struct Lnode* nextp = p;
        for (nextp = p;nextp->next != NULL; nextp = nextp->next);
        nextp->next = newone;
        p->num++;
    }
    void outelement(struct Lnode* p)
    {
        struct Lnode* nextp = p;
        for (nextp = p;nextp->next != NULL;nextp = nextp->next)
        {
            printf("[num=%d,score=%d]", nextp->next->num, nextp->next->score);
            if (nextp->next->next != NULL)
                printf("\n");
        }
    }
    int main()
    {
        struct Lnode *p;
        p = createlist();
        for (int t = 0; t < 3; t++)
        {
             addelement(p);
        }
        outelement(p);
        return 0;
    }
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月10日
  • 已采纳回答 10月2日
  • 创建了问题 10月2日

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错