C问题啊高手来急~~

来源:百度知道 编辑:UC知道 时间:2024/06/07 08:49:43
#include"stdio.h"
#include"malloc.h"
#define ST struct student
#define LEN sizeof(struct student)
struct student/*建立链接表*/
{
long num;
float score;
ST *next;
};
int n=0;
ST *create(void)
{
ST *head;
ST *p1,*p2;
p1=p2=(ST *)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(ST *)malloc(LEN);
scanf("%ld%f",p1->num,p1->score);
}
p2->next=NULL;
return head;
}/*建立链接表*/

void print(ST *head)/*输出链表的函数*/
{
ST *p;
printf("\nnow,these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld%5f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}getch();/*输出链表的函数*/

ST insert(ST *head,ST *stud) 应该改成ST* insert(ST *head,ST *stud),
scanf("%ld%f",p1->num,p1->score); 应该改成scanf("%ld%f",&p1->num,&p1->score);

针对你的问题,我调试了下,正确的程序如下,不过由于原程序的问题,似乎不能有效的终止。
改动的地方有注释
#include"stdio.h"
#include"malloc.h"
#define ST struct student
#define LEN sizeof(struct student)
struct student/*建立链接表*/
{
long num;
float score;
ST *next;
};
int n=0;
ST *create(void)
{
ST *head;
ST *p1,*p2;
float temp;/*改的*/
p1=p2=(ST *)malloc(LEN);
scanf("%ld%f",&p1->num,&temp);/*改的*/
p1->num=temp;/*改的*/
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(ST *)malloc(LEN);
scanf("%ld%f",&p1->num,&temp);/*改的*/
p1->score=temp;/*改的*/
}
p2->next=NULL;
return head;
}/*建立链接表*/

void print