要求
N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:找出学生的最高分
由函数值返回。
解题思路
通过指针指向链表,从首结点开始逐个找,找出最大值并返回。
需要用next来指向下一个结点
代码
#include <stdio.h>
#include <stdlib.h>
#define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun( STREC *h )
{
int i = 0 ;
double max = 0;
max = h->s;
//for(i=0;i<N;i++)
while(h->next != 0)
{
h=h->next;
if(max<=h->s)
{
max = h->s;
}
}
return max ;
}
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]; i++; p->next=q; p=q;
}
p->next=0;
return h;
}
outlist( STREC *h)
{ STREC *p;
p=h->next; printf("head");
do
{ printf("->%2.0f",p->s);p=p->next;}
while(p!=0);
printf("\n\n");
}
main()
{ double s[N]={85,76,69,85,91,72,64,87}, max;void NONO ();
STREC *h;
h=creat( s ); outlist(h);
max=fun( h );
printf("max=%6.1f\n",max);
NONO();
getchar();
}
void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *in, *out ;
int i,j ; double s[N],max;
STREC *h ;
in = fopen("in.dat","r") ;
out = fopen("out.dat","w") ;
for(i = 0 ; i < 10 ; i++) {
for(j=0 ; j < N; j++) fscanf(in, "%lf,", &s[j]) ;
h=creat( s );
max=fun( h );
fprintf(out, "%6.1lf\n", max) ;
}
fclose(in) ;
fclose(out) ;
}