测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A36904. 将a、b、c三个结点链成一个单向链表,并给各结点的数据域赋值,函数fun的作用是:累加链表结点数据域中的数据作为函数值返回。请改正程序中的错误,使它能得出正确的结果。#include <stdio.h>#include <stdlib.h>typedef struct list{ int data; struct list *next;} LIST;int fun(LIST *h){ LIST …

填空题 困难

题目描述

将a、b、c三个结点链成一个单向链表,并给各结点的数据域赋值,函数fun的作用是:累加链表结点数据域中的数据作为函数值返回。


请改正程序中的错误,使它能得出正确的结果。

#include <stdio.h>

#include <stdlib.h>

typedef struct list

{

    int data;

    struct list *next;

} LIST;

int fun(LIST *h)

{

    LIST *p;

    int t=0;

    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));

    system("pause");

}

参考答案

<p>1. 正确答案: while(p) 或 while(p!=NULL)</p><p>2. 正确答案: t=t+(*p).data; 或 t=t+p->data;</p>
上一题 下一题