qq_43363210
xg丶pilot
采纳率71.4%
2018-12-08 04:29 阅读 404
已采纳

大佬帮忙看下代码哪里错了

#include
#include
#include
struct student
{
int score;
int num;
struct student *next;
};
struct student *chain(int n)
{
int i;
struct student *head = NULL, *p1 = NULL, *p2 = NULL;
for(i=1;i<=n;i++)
{
if ((p1 = (struct student *)malloc(sizeof(struct student))) == NULL)
{
printf("分配失败");
exit(0);
}
if (i >= 2)p2->next = p1;
if(head = NULL)head = p2;
p2 = p1;
}
return head;
}
void input(int n, struct student *head)
{
int i;
for (i = 1; i <= n; i++)
{
printf("请输入第%d个同学的学号和成绩",i);
printf("1");
scanf("%d%d", &head->num, &head->score);
printf("2");
head = head->next;
printf("3");
}
}
void output(int n, struct student *head)
{
int i;
for (i = 1; i <= n; i++)
{
printf("%d\t%d\n", head->num, head->score);
head = head->next;
}
}
int main()
{
int n;
struct student *head;
printf("请输入学生个数n=:");
scanf("%d", &n);
head = chain(n);
input(n, head);
output(n, head);
return 0;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2018-12-08 04:56

    lz,你之前的问题的回答 http://ask.csdn.net/questions/715678 没有采纳,麻烦你采纳下,只有保持良好的采纳率,大家才愿意回答你的问题。希望以后务必注意。

    采纳的方法是点我回答左上角的采纳和向上的箭头。谢谢

    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    struct student
    {
    int score;
    int num;
    struct student *next;
    };
    struct student *chain(int n) 
    {
    int i;
    struct student *head = NULL, *p1 = NULL, *p2 = NULL;
    for(i=1;i<=n;i++)
    {
    if ((p1 = (struct student *)malloc(sizeof(struct student))) == NULL) 
    {
    printf("分配失败");
    exit(0);
    }
    p1->next = NULL;
    if (i >= 2)p2->next = p1;
    if(head == NULL)head = p1; //是==不是=,还有head=p1,因为你分配的是p1
    p2 = p1;
    }
    return head;
    }
    void input(int n, struct student *head)
    {
    int i;
    for (i = 1; i <= n; i++)
    {
    printf("请输入第%d个同学的学号和成绩",i);
    //printf("1");
    scanf("%d%d", &head->num, &head->score);
    //printf("2");
    head = head->next;
    //printf("3");
    }
    }
    void output(int n, struct student *head)
    {
    int i;
    for (i = 1; i <= n; i++)
    {
    printf("%d\t%d\n", head->num, head->score);
    head = head->next;
    }
    }
    int main()
    {
    int n;
    struct student *head;
    printf("请输入学生个数n=:");
    scanf("%d", &n);
    head = chain(n);
    input(n, head);
    output(n, head);
    return 0;
    }
    

    图片说明

    点赞 评论 复制链接分享

相关推荐