问答题 给定程序modi1.c的主函数中,将a、b、c三个结点链成一个单向链表,并给各结点的数据域赋值,函数fun()的作用是:累加链表结点数据域中的数据作为函数值返回。
请改正函数fun中指定部位的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
typedef struct list
{int data;
struct list*next;
}LIST;
int fun(LIST*h)
{LIST*p;
/**********found**********/
int t;
p=h;
/**********found**********/
while(*p)
{
/**********found**********/
t=t+p.data;
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));
}
【正确答案】
【答案解析】(1)int t=0;
(2)while(p)或while(p!=NULL)
(3)t=t+p->data。 [解析]
(1)题目中变量t是用来存放累加和的,因此必须初始化。
(2)题目中*p是结构体,不能转化为bool型。
(3)p是指针,只能用p->,不能用p.。