#include <stdio.h> #include <conio.h> #include <stdlib.h> typedef struct aa {int data; struct aa *next; }NODE; fun(NODE *h) {int max=-1; NODE *p; p=h; while(p) {if(p->data>max) max=p->dat; p=h->next; } return max; } outresult(int s,FILE *pf) {fprintf(pf,"\nThe max in link:%d\n",s);} NODE *creatlink(int n,int m) {NODE *h,*p,*s,*q; int i,x; h=p=(NODE*)malloc(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; } outlink(NODE *h,FILE *pf) {NODE *p; p=p->next; fprintf(pf,"\nTHE LIST:\n\n HEAD"); while(p) {fprintf(pf,"->%d",p->data);p=p->next;} fprintf(pf,"\n"); main() {NODE *head;int m; clrscr(); head=creatlink(12,100); outlink(head,stdout); m=fun(head); printf("\nTHE RESULT:\n");outresult(m,stdout); }
1.在creatlink函数中,p->next还没有赋初值,却有“s->next=p->next;”,p->next指向谁还不确定啊? 2.主函数中,“ outlink(head,stdout);”,stdout是什么?如果是变量的话怎么还没有定义就使用了?
----------------解决方案--------------------------------------------------------
下面回答楼主的问题: 1.在creatlink函数中, 令p->next =NULL, 具体位置请看我下面的程序. 2.stdout 代表标准输出,即是指显示器(屏幕).如果你不想用这个的话,请使用我程序中的printf(). //1.在creatlink函数中,p->next还没有赋初值,却有“s->next=p->next;”,p->next指向谁还不确定啊? //2.主函数中,“ outlink(head,stdout);”,stdout是什么?如果是变量的话怎么还没有定义就使用了?
#include <stdio.h> #include <stdlib.h>
typedef struct node { int data; struct node *next; }NODE;
int fun(NODE *h) { int max=-1; NODE *p; p=h; while(p) { if(p->data>max) max=p->data; p=p->next; } return max; }
void outresult(int s,FILE *pf) { //fprintf(pf,"\nThe max in link:%d\n",s); printf("\nThe max in link:%d\n",s); }
NODE *creatlink(int n,int m) { NODE *h,*p,*s; int i; h=p=(NODE*)malloc(sizeof(NODE)); p->next=NULL; 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; //fprintf(pf,"\nTHE LIST:\n\n HEAD"); printf("\nTHE LIST:\n\n HEAD"); while(p) { //fprintf(pf,"->%d",p->data); printf("->%d",p->data); p=p->next; } //fprintf(pf,"\n"); printf("\n"); }
void main() { NODE *head; int m; head=creatlink(12,100); outlink(head,stdout); m=fun(head); printf("\nTHE RESULT:\n"); outresult(m,stdout); }
----------------解决方案--------------------------------------------------------
不要忘了送我积分哦
----------------解决方案--------------------------------------------------------
楼上的这么辛苦,一定要给积分啊。不能说话不算数哦。
----------------解决方案--------------------------------------------------------
但原来的程序中没有“p->next=NULL",却一样可以运行,这是为什么啊?
----------------解决方案--------------------------------------------------------
p->next默认为空
----------------解决方案--------------------------------------------------------