题目内容
程序改错:下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数fun的功能是将单向链表结点(不包括头结点)数据域为奇数的值累加起来,并且作为函数值返回。#include #include #include typedef struct aa{int data;struct aa *next;} NODE;int fun (NODE *h){int sum=0;NODE *p;p=h->next;/***********【1】FOUND***********/while(p->next){if(p->data%2==1)sum+=p->data;/***********【2】FOUND***********/p=h->next;}return sum;}NODE *creatlink(int n){NODE *h,*p,*s;int i;h=p=(NODE*)malloc(sizeof(NODE));for(i=1;idata=rand()%16;s->next=NULL;p->next=s;p=p->next;}p->next=NULL;/***********【3】FOUND***********/return p;}void outlink(NODE *h){NODE *p;/***********【4】FOUND***********/p=h;printf("\n\n The LIST :\n\n HEAD");while(p){printf("->%d",p->data);p=p->next;}printf("\n");}int main(){NODE *head; int sum;system("CLS");head=creatlink(10);outlink(head);/***********【5】FOUND***********/fun(head);printf("\nSUM=%d",sum);return 0;}
查看答案
搜索结果不匹配?点我反馈
更多问题