#include <stdio.h>
#include <stdlib.h>
#define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun( STREC *h )
{
STREC *p;
double sum=0;
p=h;
while(p!=NULL)
{
sum+=p->s;p=p->next;
}
return sum/N;
}
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("->%4.1f",p->s); p=p->next;
}while(p!=0);
printf("\n\n");
}
void main()
{ double s[N]={85,76,69,85,91,72,64,87},ave;
STREC *h;
h=creat( s );
outlist(h);
ave=fun( h );
printf("ave= %6.3f\n",ave);
}