代码如下,如有帮助,请采纳一下,谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct StNode
{
int num;
char name[20];
int score;
StNode* next;
};
int main()
{
int num,score,i;
char name[20]={0};
struct StNode* head,*tmp,*p,*max;
head = (struct StNode*)malloc(sizeof(struct StNode));
printf("请输入学生1的学号:");
scanf("%d",&num);
printf("请输入学生1的姓名:");
scanf("%s",name);
printf("请输入学生1的成绩:");
scanf("%d",&score);
head->num = num;
strcpy(head->name,name);
head->score = score;
head->next = 0;
p = head;
for (i=1;i<5;i++)
{
tmp = (struct StNode*)malloc(sizeof(struct StNode));
printf("请输入学生%d的学号:",i+1);
scanf("%d",&num);
printf("请输入学生%d的姓名:",i+1);
scanf("%s",name);
printf("请输入学生%d的成绩:",i+1);
scanf("%d",&score);
tmp->num = num;
strcpy(tmp->name,name);
tmp->score = score;
tmp->next = 0;
p->next = tmp;
p = tmp;
}
//遍历并查找最大值
p = head;
max = head;
while(p)
{
if (p->score > max->score)
{
max = p;
}
p = p->next;
}
printf("成绩最大的学生信息为:%d %s %d\n",max->num,max->name,max->score);
//释放资源
p = head;
while(p)
{
tmp = p->next;
free(p);
p = tmp;
}
return 0;
}
定义一个学生结构,带一个next指针,根据5个学生信息形成链表,记录头指针head,遍历搜索计算统计结果
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
char name[20];
float score;
student *next;
};
struct student *head = NULL;
struct student* createlink()
{
for(int i=0;i<5;i++)
{
printf("请输入第%d个学生信息:",i+1);
struct student *p = (student*)malloc(sizeof(struct student));
scanf("%d %s %f",&p->num,p->name,&p->score);
if(head == NULL)
head = p;
else
{
p->next = head;
head = p;
}
}
return head;
}
struct student *getmax()
{
struct student *p = head;
struct student *pMax = NULL;
while(p != NULL)
{
if(pMax == NULL)
pMax = p;
else if(pMax->score < p->score)
pMax = p;
}
return pMax;
}
int main()
{
head = createlink();
struct student *pMax = getmax();
if(pMax != NULL)
{
printf("学号:%d\n",pMax->num);
printf("姓名:%s\n",pMax->name);
printf("成绩:%g\n",pMax->score);
}
return 0;
}