填空题
下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数proc()的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。请修改程序中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
typedef struct aa
{ int data;
struct aa*next;
}NODE;
//****found****
proc(NODE*h)
{int max=-1;
NODE*p;
p=h->next;
while(p)
{ if(p->data>max)
max=p->data;
//****found****
p=h->next;
}
return max;
void outresult(int s,FILE*pf)
{ fprintf(pf,"/nThe max in link:%d/n",s);}
NODE*creatlink(int n,int m)
{ NODE*h,*p,*s;
int i;
srand((unsigned)time(NULL));
h=p=(NODE*)malloe(sizeof(NODE));
h->data=9999;
for(i=1;i<=n;i++)
{ s=(NODE*)malloc(sizeof(NODE));
s->data=rand()%m;s->next=p->
next;
p->next=s;p=p->next;
}
p->next=NULL;
return h;
}
void outlink(NODE*h,FILE*pf)
{ NODE*p;
p=h->next;
fprintf(pf,"/n The LIST:/n/n HEAD");
while(p)
{ fprintf(pf,"->%d",p->data);
p=p->next;}
fprintf(pf,"/n");
}
void main()
{ NODE*head;int m;
system("CLS");
head=creatlink(12,100);
outlink(head,stdout);
m=proc(head);
printf("/nThe RESULT:/n");outresult
(m,stdout);
}