给定程序modil.c的主函数中,将a、b、c三个结点链成一个单向链表,并给各结点的数据域赋值,函数fun()的作用是:累加链表结点数据域中的数据作为函数值返回。 请改正函数fun中指定部位的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增

admin2020-11-27  23

问题 给定程序modil.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;改为int t=0; (2)*p改为p或者p!=NULL (3)p.data改为p->data

解析 (1)int t;改为int t=0;题目中变量t是用来存放累加和的,必须初始化。
(2)*p改为p或者p!=NULL,题目中*p是结构体,不能转化为bool型;
(3)p.data改为p->data,p是指针,只能用p->,不能用p.。
转载请注明原文地址:https://kaotiyun.com/show/Qstp777K
0

最新回复(0)