C语言 解释下这个程序

来源:百度知道 编辑:UC知道 时间:2024/05/19 19:41:51
struct node{
int num;
struct node *next;
};
int fun(struct node *h)
{int k=0;
struct node *p=h,*q=NULL;
while(p){
if ((q!=NULL)&&(p->next)) k+=p->num;
q=p;
p=p->next;
}
return k;
}
写出执行语句"printf("%d\n",fun(head));"的输出结果。
答案:14

struct node{
int num;
struct node *next;
};//声明链表
int fun(struct node *h)//从第h个指针位置累加链表中存储数字的和,并用k返回
{int k=0;
struct node *p=h,*q=NULL;
while(p){
if ((q!=NULL)&&(p->next)) k+=p->num;
q=p;
p=p->next;
}
return k;
}
写出执行语句"printf("%d\n",fun(head));"的输出结果就是输出整个链表中存储数的总和.

struct node{
int num;
struct node *next;
}; \\定义结构体 node node里面包含 int型嘚
\\num 和 struct node 型嘚 指针 next
int fun(struct node *h) \\函数fun有struct node类型
\\嘚参数
{int k=0; \\k 统计和
struct node *p=h,*q=NULL;\\定义struct node类型嘚
\\指针 p指向h;q为空
while(p){ \\循环直到 q为空或p->next为空
if ((q!=NULL)&&(p->next)) k+=p->num; \\统计和
q=p;
p=p->next;
}
return k;
}

没主函数啊main() 不能运行嘚