代码如下:
#include <alloc.h>
#include "stdio.h"
struct node {long num;
float grade;
struct node *next;
};
struct node *creatlink()
{int k;
long nn;
struct node *head,*p,*q;
head=NULL;k=0;
printf("\nPlease input one students number (0 is over):");
scanf("%ld",&nn);
while (nn!=0L)
{ k++;
p=(struct node *)malloc(sizeof(struct node));
p->num=nn;
printf("\nPlease input grade:");
scanf("%f",&p->grade);
if(k==1) head=p;
else q->next=p;
q=p;
printf("\nPlease input next students number(0 is over):");
scanf("%ld",&nn);
}
q->next=NULL;
return(head);
}
void outputlink(head)
struct node *head;
{ struct node *p;
if (head==NULL)
{printf("\n NULL LINK");
return;
}
printf("\n NUMBER GRADE");
p=head;
while(p!=NULL)
{
printf("\n%8ld%10.1f",p->num,p->grade);
p=p->next;
}
}
main()
{struct node *head,*stu;
head=creatlink();
outputlink(head);
}
一个警告如下:Possible use of q before definition in function creatlink
无错误;
但运行后:输入任意一个整数后回车,按ALT+F5后出现:
please input grade:scanf: floating point formats not linked
Abnormal program termination
各位大虾帮忙看看问题出在何处。俺在此跪谢;
注:俺是一只超菜鸟,希望仁兄们提拔
几个小地方改正后如下
#include <stdlib.h>
#include <stdio.h>
struct node {long num;
float grade;
struct node *next;
};
struct node *creatlink()
{int k;
long nn;
struct node *head,*p,*q;
head=NULL;k=0;
printf("\nPlease input one students number (0 is over):");
scanf("%ld",&nn);
while (nn!=0L)
{ k++;
p=(struct node *)malloc(sizeof(struct node));
p->num=nn;
printf("\nPlease input grade:");
scanf("%f",&p->grade);
if(k==1) head=p;
else q->next=p;
q=p;
printf("\nPlease input next students number(0 is over):");
scanf("%ld",&nn);
}
q->next=NULL;
return(head);
}
void outputlink(struct node *head)
{ struct node *p;
if (head==NULL)
{printf("\n NULL LINK");
return;
}
printf("\n NUMBER GRADE");
p=head;
while(p!=NULL)
{
printf("\n%8ld%10.1f",p->num,p->grade);
p=p->next;
}
}
void main()
{struct node *head,*stu;
head=creatlink();
outputlink(head);
}
在vc6通过!
whmily (不回头的土豆) 。
这位仁兄是用Turbo c 2.0调试的吧
Turbo c 2.0有个bug
这是编译器的原因。
对结构体中的浮点数付值。会出错。
你的程序没有错。
你可以用Vc++测你的程序就行了。
什么都没改,Dev C++通过~~~
不过看你的给出的警告,好像是说scanf()函数不支持对成员数据的直接赋值~~~
:Possible use of q before definition in function creatlink
while (nn!=0L)
{
......
}
q->next=NULL;
当nn一开始就等于0的时候。
q根本就不存在。也就是没有指向确切的地址。
q->next就不成立。