N名学生的成绩已在主函数中放入一个带头节点的链表结构体中,h指向链表的头节点。fun函数的功能是:找出学生的最高分,由函数值返回。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#define N 8
struct slist
{
double s;
struct slist *next;
};
typedef struct slist STREC;
double fun(STREC *h)
{
double s = 0;
STREC *p = h->next;
while (p)
{
if (s < p->s)
s = p->s;
p = p->next;
}
return s;
}
STREC *creat(double *s)
{
STREC *h, *p, *q;
int i = 0;
h = p = (STREC*)malloc(sizeof(STREC));
p->s = 0;
while (i < N)
{
q = (STREC*)malloc(sizeof(STREC));
q->s = s[i];
p->next = q;
p = q;
i++;
}
p->next = NULL;
return h;
}
outlist(STREC *h)
{
STREC *p;
p = h->next;
printf("head");
do
{
printf("->%2.0f", p->s);
p = p->next;
} while (p);
printf("\n\n");
}
int main()
{
double s[N] = { 85,76,69,85,91,72,64,87 },max;
STREC *h;
h = creat(s);
outlist(h);
max = fun(h);
printf("max=%6.1f\n", max);
system("pause");
return 0;
}