首页 > 代码库 > P164

P164

#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)
	{	s+=p->data;
		p=p->next;
	}
	return s;
}
main()
{
	SLIST *head;
	int a[N]={268,87,45,32,91,16,20,12};
	head=creatlist(a);outlist(head);
	printf("\nsum=%d\n",fun(head));
}

SLIST *creatlist(int a[])
{
	SLIST *h,*p,*q;int i;
	h=p=(SLIST *)malloc(sizeof(SLIST));
	for(i=0;i<N;i++)
	{	q=(SLIST *)malloc(sizeof(SLIST));
		q->data=http://www.mamicode.com/a[i];p->next=q;p=q;>没看懂  开学再看看