给定程序中,函数 fun 的功能是计算出带有头结点的单向链表中各结点数据域之和作为函数值返回。
#include<stdio.h>
#include<stdlib.h>
#define N 8
typedef struct list{
int data;
struct list *next;
}SLIST;
SLIST *creatlist(int *);
void outlist(SLIST *);
int fun(SLIST *h){
SLIST *p;
int s=0;
p=h->next;
while(p){
/**********found**********/
s+=p->data;
/**********found**********/
p=p->next;
}
return s;
}
void main(){
SLIST *head;
int a[N]={12,87,45,32,91,16,20,48};
head=creatlist(a);
outlist(head);
/***********found**********/
printf("sum=%d\n",fun(head));
}
SLIST *creatlist(int a[]){
SLIST *h,*