请教大神,运行后只打印一行,就提示出现问题,导致程序停止正常工作。
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student) //student结构的大小
struct student *creat(); //创建链表
void print(struct student *head); //打印链表
struct student
{
int num;
float score;
struct student *next;
};
int n;//全局变量,用来记录存放了多少数据
void main()
{
struct student *stu;
stu=creat();
print(stu);
printf(" ");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1 = p2 = (struct student *)malloc(LEN); //LEN是student结构的大小
printf("Please enter the num:");
scanf("%d",&p1->num);
printf("Please enter the score :");
scanf("%f",&p1->score);
head=NULL;
n=0;
while(p1->num)
{
n++;
if(1==n)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=p1=p2=(struct student *)malloc(LEN);
printf(" Please enter the num:");
scanf("%d",&p1->num);
printf("Please enter the score :");
scanf("%f",&p1->score);
}
return head;
}
void print(struct student *head)
{
struct student *p;
printf(" There are %d records! ",n);
p=head;
if( NULL != head )
{
do
{
printf("学号为%d的成绩是:%f ",p->num,p->score);
p=p->next;
}while(NULL != p );
}
}
1。 p1=p1=p2=(struct student *)malloc(LEN);
只要 p1=(struct student *)malloc(LEN);
就可以了
2。关键:
return head;
前面必须有
p2->next=NULL;
否则,链表没有结尾了
最好不要用修改器,有病毒的,你安装的时候杀毒软件肯定提醒你了,是否允许或者阻止这个软件干嘛干嘛的,出现这种现象是因为安装的时候少了东西,然后你运行这个软件的时候,运行什么功能,这个软件就一直在找啊找,然后一直找不到,就发生你这个提示,在加上它软件没有对这种找不到东西的情况进行处理,建议安装的时候关闭杀毒软件。
下一篇:这电脑配置能玩孤岛惊魂5吗?