📄 modi41.c
字号:
/*
下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数fun的功能是将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并且作为函数值返回。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构子子。
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct aa
{
int data ;
struct aa *next;
}NODE;
int fun(NODE *h)
{ int sum=0;
NODE *p;
p=h;
/**********found************/
while(p)
{ if(p->data%2==0)
sum+=p->data;
/**********found************/
p=h->next;
}
return sum;
}
NODE *creatlink(int n)
{ NODE *h,*p,*s,*q;
int i,x;
h=p=(NODE*)malloc(sizeof(NODE));
for(i=1;i<=n;i++)
{ s=( NODE*)malloc(sizeof(NODE));
s->data=rand()%16;
s->next=p->next;
p->next=s;
p=p->next;
}
p->next=NULL;
return h;
}
outlink(NODE *h,FILE *pf)
{ NODE *p;
p=h->next;
fprintf(pf,"\n\nTHE LiST :\n\n HEAD");
while(p)
{fprintf(pf,"->%d",p->data);p=p->next;}
fprintf(pf,"\n");
}
outresult(int s,FILE *pf)
{fprintf(pf,"\nThe sum of even numbers :%d\n",s);}
main()
{ NODE *head ;int even;
clrscr();
head=creatlink(12);
head->data=9000;
outlink(head,stdout);
even=fun(head);
printf("\nThe result :\n");outresult(even,stdout);
}
/*
答案:
while(p)改为 while(p!=NULL)
p=h->next;改为 p=p->next;
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -