当前位置: 代码迷 >> C语言 >> 程序有点问题,看不明白。
  详细解决方案

程序有点问题,看不明白。

热度:105   发布时间:2006-03-16 17:28:00.0
程序有点问题,看不明白。

程序能运行,就是输入时,刚输一数字确定就结束程序的,提示也看不明白。
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};

int n;
/*以下代码创建链表*/
struct student *creat(void)
{struct student *head,*p1,*p2;
p1=p2=(struct student *)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=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}

/*此函数输出链表中的所有值*/
void print(struct student *head)
{struct student *p;
printf("\nTHESE %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%ld,%5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}

/*下面为主函数*/
main()
{struct student *pp;
pp=creat();
print(pp);
}


----------------解决方案--------------------------------------------------------
真的很烦,这个问题不知道回答过几次,这次俺就是不说
----------------解决方案--------------------------------------------------------
交高手朋友
----------------解决方案--------------------------------------------------------
struct student *creat(void)
{struct student *head,*p1,*p2;
p1=p2=(struct student *)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=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}

代码中你应该没有将第一个节点与head连起来所以后面的都没用了
不过我也是新手 错了请见谅!呵呵
----------------解决方案--------------------------------------------------------
好奇怪哦最近C语言论坛里面怎么出现了这么多数据结构的问题啊
----------------解决方案--------------------------------------------------------
楼主,俺不明白,这程序我运行了,好象没什么问题啊,,俺用的是C-FREE编译的
----------------解决方案--------------------------------------------------------
  相关解决方案