下列给定程序中,函数fun的功能是:计算一个带头结点的单向链表中各结点的数据域中数值之和,结果作为函数值返回。 请在程序的下画线处填入正确的内容并将下画线删除,使程序得出正确的结果。 注意:部分源程序给出如下。 不得增行或删行,也不

admin2015-11-24  2

问题 下列给定程序中,函数fun的功能是:计算一个带头结点的单向链表中各结点的数据域中数值之和,结果作为函数值返回。
    请在程序的下画线处填入正确的内容并将下画线删除,使程序得出正确的结果。
    注意:部分源程序给出如下。
    不得增行或删行,也不得更改程序的结构!
    试题程序:
#include<stdio.h>
#include<stdlib.h>
#define N 8
typedef struct list:
{  int data;
    struct list *next:
}SLTST;
SLIST*creatlist(int*);
void outlist(SLIST*);
int fun(SLIST*h)
{  SLIST*p;    int s=0;
    P=h一>next;
    while(P)
    {
/**********found***********/
  s+=p一>【1】
/**********found***********/
  p=p一>【2】
    }
    return s:
}
main()
{  SLIST*head;
  int a[N]={12,87,45,32,91,16,20,48};
    head=creatlist(a);
    outlist;(head);
/**********found***********/
    printf(“\nsum=%d\n”,fun
(【3】));
}
SLIST*creatlist:(int a[])
{  SLIST*h,*p,*q;   int i;
    h=p=(SLIST*)malloc(sizeof
(SLTST));
    for(i=0;i<N,i++)
    {q=(SLIST*)malloc(sizeof
(SLIST));
    q一>data=a,P一>next=q;
    p=q;
    }
    p一>next=0;
    return h;
}
void outlist(SLIST*h)
{  SLIST*p;
    p=h一>next;
    if(P==NULL)
    printf(“The list is NULL!\n”);
    else
    {printf(“\nHead”),
    do
    { printf(“一>%d”,P一>data);
    p=p一>next;}
    while(P!=NULL),
    printf(“一>End\n”);
    }
}

选项

答案(1)data (2)next: (3)head

解析 填空1:变量s用来累加各结点的数据域,因此此空应为data。
    填空2:每次循环结束时,指针p指向下一个结点,即“p=p一>next;”。
    填空3:由被调用函数的形参列表可知,此处应为指针类型变量,因为要对链表的数据域求和,所以将链表的头指针传给被调用函数。
转载请注明原文地址:https://www.kaotiyun.com/show/BVID777K
0

相关试题推荐
最新回复(0)