链表怎么运行不了
# include <stdio.h># include <malloc.h>
# define LEN sizeof(struct Student)
struct Student
{
long num;
float socre;
struct Student * next;
};
int n;//定义全局变量n,n是结点个数
struct Student * creat()//建立链表函数
{
struct Student * head;
struct Student *p1,*p2;
n = 0;
p1 = p2 = (struct Student *)malloc(LEN);
scanf("%ld,%f",p1->num ,p1->socre );
head = NULL;
while( p1->num != 0)
{
n = n+1;
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct Student *)malloc(LEN);
scanf("%ld,%f",&p1->num ,&p1->socre );
}
p2->next = NULL;
return (head);
}
void print(struct Student * head)//输出链表函数
{
struct Student* p;
printf("\nNow,These %d records are:\n",n);
p = head;
if(head != NULL)
do
{
printf("%ld %5.1f\n",p->num ,p->socre );
p = p->next;
}while(p != NULL);
}
int main()
{
struct Student * head;
head = creat();
print( head);
return 0;
}
//一运行就应用程序错误,
# include <stdio.h>
# include <malloc.h>
# define LEN sizeof(struct Student)
struct Student
{
long num;
float socre;
struct Student * next;
};
int n;//定义全局变量n,n是结点个数
struct Student * creat()//建立链表函数
{
//struct Student * head;
struct Student * he;
struct Student *p1,*p2;
n = 0;
p1 = p2 = (struct Student *)malloc(LEN);
//scanf("%ld,%f",p1->num ,p1->socre );
scanf("%ld,%f",&p1->num ,&p1->socre );
he = NULL;
while( p1->num != 0)
{
n = n+1;
/*
if(n == 1)
head = p1;
else
p2->next = p1;
p2 = p1;
p1 = (struct Student *)malloc(LEN);
scanf("%ld,%f",&p1->num ,&p1->socre );
*/
if(n == 1)
{
he = p1;
}
else
{
p2 = (struct Student *)malloc(LEN);
p1->next = p2;
scanf("%ld,%f",&p2->num ,&p2->socre );
p1 = p2;
}
}
//p2->next = NULL;
p1->next= NULL;
return (he);
}
void print(struct Student * head)//输出链表函数
{
struct Student* p;
printf("\nNow,These %d records are:\n",n);
p = head;
if(head != NULL)
do
{
printf("%ld %5.1f\n",p->num ,p->socre );
p = p->next;
}while(p != NULL);
}
int main()
{
struct Student * head;
head = creat();
print( head);
return 0;
} 牡丹花下死做鬼 发表于 2014-9-27 09:24
谢谢了,发现问题了,我这里scanf("%ld,%f",p1->num ,p1->socre );
后面少了2个位取于符号,但是很奇怪,居然编译没提示错误,
页:
[1]