首页
社区
课程
招聘
[求助]C链表处理问题!
发表于: 2007-1-25 15:37 4177

[求助]C链表处理问题!

2007-1-25 15:37
4177
程序如下:
#include "stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *cread(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
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);
  printf("the secend recore:");
  scanf("%ld%f",&p1->num,&p1->score);
}
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->score);
     p=p->next;
    }while(p!=NULL);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL){printf("\nno list!\n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
  {
   if(p1==head)
   head=p1->next;
   else p2->next=p1->next;
   printf("delete %ld\n",num);
   n=n-1;

  }
else printf("%ld not been found !\n",num);
end: return(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
  {while((p0->num>p1->num)&&(p1->next!=NULL))
   {p2=p1;
    p1=p1->next;
   }
   if(p0->num<p1->num)
   {if(head==p1)head=p0;
   else p2->next=p0;
   p0->next=p1;
   }
   else
   {p1->next=p0;p0->next=NULL;
   }
}
   n=n+1;
   return(head);

}
main()
{
struct student *head,*stu;
long del_name;
printf("input records:\n");
head=cread();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_name);
while(del_name!=0)
{head=del(head,del_name);
  print(head);
  printf("input the delete num:");
  scanf("%ld",&del_name);
}
printf("\ninput the inserted recore:");
stu=(struct student * )malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
  {head=insert(head,stu);
  print(head);

printf("input the insert record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
}

编译.连接通过,在执行时:输入学生序号与成绩时,只能得到学生序号,而不能得到成绩,也就是说scanf没有完成指定工作,,不知道那里问题,是tc2.0,XP系统,
输入101时可正常运行,如输入101,89时,就退出提示如下"
scanf: floating point formats not linked
Abnormal program terminstion
不知道什么意思,是不是我creat函数里用scanf用的不对啊,,用了这么多次了,应该不会啊.
  F7进入:
struct student *cread(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
head=NULL;
查p1->num时,为正常,p1->score时为:1.1111111156484.
具体情况就这样了.

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 207
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
没人看啊?
2007-1-27 09:30
0
雪    币: 240
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
scanf()函数的默认间隔符是空白字符,也就是说你在输入时要用101 89或101<回车>89代替101,89。
如果非要用“,”,这样写scanf("%d,%f",&p->num,&p->score);
2007-1-27 13:27
0
游客
登录 | 注册 方可回帖
返回
//