解题思路:
注意事项:
第一次自己设置好链表,运行成功有点激动。别根据输出提示输入就行,不然运行不成功。
参考代码:
#include <stdio.h>
#include <stdlib.h>
struct student
{
char number[20];
char name[20];
int grade1;
int grade2;
int grade3;
struct student* next;
};
void CreatList(struct student* head,int N);
void input(struct student* temp);
void print(struct student* head);
int main(void)
{
struct student* head=(struct student*)malloc(sizeof(struct student));
int N; //N是学生记录数目
scanf("%d",&N);
CreatList(head,N);
print(head);
return 0;
}
/*----------------设置头结点-------------*/
void CreatList(struct student* head,int N)
{
struct student* move=head;
for(int i=1;i<=N;i++)
{
struct student* temp=(struct student*)malloc(sizeof(struct student));
move->next=temp;
input(temp);
temp->next=NULL;
move=temp;
}
}
void input(struct student* temp)
{
// printf("输入学号");
scanf("%s",temp->number);
// printf("输入名字");
scanf("%s",temp->name);
//printf("输入第一科成绩");
scanf("%d",&temp->grade1);
// printf("输入第二科成绩");
scanf("%d",&temp->grade2);
// printf("输入第三科成绩");
scanf("%d",&temp->grade3);
}
void print(struct student* head)
{
struct student* move=head->next;
while(move!=NULL)
{
printf("%s,%s,%d,%d,%d",move->number,move->name,move->grade1,move->grade2,move->grade3);
printf("\n");
move=move->next;
}
}
0.0分
1 人评分