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

#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个回答

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

图片说明

qq_43363210
xg丶pilot 谢谢
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问