定义一个函数,用于建立一个含有5个学生,每个学生3门功课成绩的链表,主函数调用该函数,然后输出链表数据

要求写出全部C语言程序
2025-04-07 19:24:13
推荐回答(1个)
回答1:

头文件stdio和stdlib
typedef struct Student
{
int chinese;
int math;
int english;
struct Student *next;
}Student;
typedef struct Student *LinkList;

void CreatListTail(LinkList *L)
{
LinkList p=NULL,r=NULL;
int i=0;
*L=(LinkList)malloc(sizeof(Student));
r=*L;
for(i=0;i<5;i++)
{
p=(Student *)malloc(sizeof(Student));
p->chinese=55+i*5;
p->math=90+i;
p->english=75+i*2;
r->next=p;
r=p;
}
r->next=NULL;
}

int main()
{
LinkList stu=(LinkList)malloc(sizeof(Student)),p=NULL;
if(!stu)
printf("内存分配失败\n");
stu->next=NULL;
CreatListTail(&stu);
p=stu->next;
while(p)
{
printf("语文%d 数学%d 英语%d\n",p->chinese,p->math,p->english);
p=p->next;
}
p=NULL;

return 0;
}
希望能帮到你