下面是我写的程序,在tc里边可以运行,但是在vc6里边报内存错误,请大侠指点!
/*Note:YourchoiceisCIDE*/#include<stdio.h>#include<stdlib.h>structStudent{longnum;flo...
/* Note:Your choice is C IDE */
#include<stdio.h>
#include<stdlib.h>
struct Student
{
long num;
float score;
struct Student *next;
};
//int n=0;
struct Student *creat() //建立链表函数//
{
long num1;
float score1;
struct Student *link,*p1,*p2;
link=(struct Student*)(sizeof(struct Student));
link->next=NULL;
p1=link;
p2=(struct Student * )malloc(sizeof(struct Student));
printf("please input the num and score!\n");
scanf("%ld,%f",&num1,&score1);
while((num1!=0)&&(score1!=0))
{
p2->num=num1;
p2->score=score1;
p2->next=NULL;
p1->next=p2;
p2->next=NULL;
p1=p1->next;
printf("please input the num and score!\n");
scanf("%ld,%f",&num1,&score1);
}
return link;
}
void print(struct Student *link)
{
struct Student *p;
// printf("\nNow,These%d records are:\n",n);
p=link;
while(p!=NULL)
{
printf("%ld%f\n",p->num,p->score);
p=p->next;
}
}
void main() //主函数//
{
struct Student *head;
head=creat(); //建立链表函数//
print(head); //调用函数出错 //输出链表以及表长函数//
} 展开
#include<stdio.h>
#include<stdlib.h>
struct Student
{
long num;
float score;
struct Student *next;
};
//int n=0;
struct Student *creat() //建立链表函数//
{
long num1;
float score1;
struct Student *link,*p1,*p2;
link=(struct Student*)(sizeof(struct Student));
link->next=NULL;
p1=link;
p2=(struct Student * )malloc(sizeof(struct Student));
printf("please input the num and score!\n");
scanf("%ld,%f",&num1,&score1);
while((num1!=0)&&(score1!=0))
{
p2->num=num1;
p2->score=score1;
p2->next=NULL;
p1->next=p2;
p2->next=NULL;
p1=p1->next;
printf("please input the num and score!\n");
scanf("%ld,%f",&num1,&score1);
}
return link;
}
void print(struct Student *link)
{
struct Student *p;
// printf("\nNow,These%d records are:\n",n);
p=link;
while(p!=NULL)
{
printf("%ld%f\n",p->num,p->score);
p=p->next;
}
}
void main() //主函数//
{
struct Student *head;
head=creat(); //建立链表函数//
print(head); //调用函数出错 //输出链表以及表长函数//
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询