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

admin2016-04-07  26

问题 给定程序modil.C的主函数中,将a、b、c三个节点链成一个单向链表,并给各节点的数据域赋值,函数fun()的作用是:累加链表节点数据域中的数据作为函数值返回。
  请改正函数fun中指定部位的错误,使它能得出正确的结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
  试题程序:
  #include
  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()
    {UsT a,b,c,*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)将while(*p)中的*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://jikaoti.com/ti/Ndi0FFFM
0

最新回复(0)