64,686
社区成员
发帖
与我相关
我的任务
分享
#include "stdio.h"
#include "stdlib.h"
struct stu
{
int index;
float score;
struct stu *next;
};
void CreateList(struct stu **head)//--------->传二级指针
{
struct stu *temp=(*head); //------->here将头指针赋给一个临时变量
int i=1;
printf("请输入5位学生的成绩:\n");
while(i<=5)
{
struct stu *newNode=(struct stu*)malloc(sizeof(struct stu)); //为新结点分配空间
newNode->next=NULL; //新节点next域赋值为NULL
newNode->index=i++; //为新结点赋值
scanf("%f",&newNode->score);
temp->next=newNode; //将新结点连到链表末尾
temp=newNode; //修改临时指针指向新结点
}
}
void main()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
head->next=NULL;
CreateList(&head);//----->here
struct stu *t = head->next;
while (t)
{
printf("%d %.2f\n", t->index, t->score);
t=t->next;
}
}
//以下是一个结果
请输入5位学生的成绩:
66.7
55.8
99.8
34.5
67.98
1 66.70
2 55.80
3 99.80
4 34.50
5 67.98
Press any key to continue
scanf("%f",&(newNode->score));
scanf("%f",&(newNode->score));//把输入改下就好了