关于结构体和链表结构

程序如下

#include <stdio.h>
typedef struct list
{ int data;
struct list *next;
} LIST;
int fun(LIST *h)
{ LIST *p;
int t = 0;//初始值不给的话会出现很离奇的错误!
p=h;
while( p )//*p为结构体,不能转换为bool(布尔)型,只能用p或者*p!=NULL
{
t=t+p->data;//p为指针,需要用->,不能用 . 
p=(*p).next;
}
return t;
}
main()
{ LIST a, b, c, *h;
a.data=34; b.data=51; c.data=87; c.next='\0';
h=&a; a.next=&b; b.next=&c;
printf("总和 = %d\n",fun( h ));
}

 

作用如下:将a,b,c三个节点连接成一个单向链表,并给各界定啊的数据域赋值,函数fun()的作用是:累加链表结点数据域中的数据作为函数值返回。

程序来源:c语言题库二级c语言程序设计-创2-43.

2016-03-20

posted @ 2016-03-20 01:18  a2582069  阅读(206)  评论(0编辑  收藏  举报